Changing a field value before save

Questions related to customising nuBuilder Forte with Javascript or PHP.

Changing a field value before save

Postby ahernandez » Tue May 15, 2018 4:13 am

Hi,

I am trying to change a field value in BB php section but it doesn't work.
Any idea?

--------------------------------------
$ejercicio = substr(nuHash()[opo_fecha],0,4);
$s = "select cod_oportunidad from codigo where cod_ejercicio = $ejercicio";

$t = nuRunQuery($s);
$r = db_fetch_object($t);

nuSetFormValue('opo_oportunidad', $r->cod_oportunidad); // The value in $r->cod_oportunidad is right but the field 'opo_oportunidad' doesn't get the new value.
--------------------------------------

Thanks.
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Re: Changing a field value before save

Postby toms » Tue May 15, 2018 2:07 pm

ahernandez wrote:Hi,
I am trying to change a field value in BB php section but it doesn't work.
Thanks.


Don't you mean BS (Before Save) PHP section ?

BTW, I don't think that values can be modified with nuSetFormValue() in BS. You probably need to modify the POST data. I'm sure Steven can tell you how to do it.
toms
 

Re: Changing a field value before save

Postby ahernandez » Tue May 15, 2018 3:43 pm

The problem is I have to set a field value 'opo_oportunidad' in the last moment before insert the record in database and it depends of the year typed in form date field. That's why I looking the last number used for this year in the table 'codigo'. Obviously after this I have to update this record in 'codigo' as well...
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Re: Changing a field value before save

Postby ahernandez » Tue May 15, 2018 3:45 pm

Sorry toms,

I am doing this in BS section, not in BB. It was a mistake.
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Re: Changing a field value before save

Postby admin » Wed May 16, 2018 6:47 am

ahernandez,

I would let nuBuilder save the record first and then update the record(s) you want in After Save with nuRunQuery().


Steven
admin
Site Admin
 
Posts: 2970
Joined: Mon Jun 15, 2009 9:53 am

Re: Changing a field value before save

Postby ahernandez » Wed May 16, 2018 1:48 pm

Good Morning Steven,

By trial and error I have reached that conclusion and now it works fine.
For the moment, in one way or another, I am managing to save all the problems that arise.
Step by step this is taking shape!

thank you very much for your help.
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Re: Changing a field value before save

Postby ahernandez » Wed May 16, 2018 1:50 pm

By the way...

How to control if nuRunQuery has failed updating or inserting records?
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Re: Changing a field value before save

Postby toms » Wed May 16, 2018 3:29 pm

You can try nuErrorFound()

This function returns true if an error has occurred. This is used to ensure that if an error occurs the program is able to stop before committing erroneous data to the database.


From the v3 Wiki, not documented on the v4 wiki. It might still work in v4'

Usage:
Code: Select all
  $t = nuRunQuery($sql);
      
        if (nuErrorFound()) {
            return;
        }
toms
 

Re: Changing a field value before save

Postby admin » Thu May 17, 2018 8:25 am

ahernandez,

SQL errors will show up in Debug Results.

debug.png
debug.png (9.2 KiB) Viewed 454 times


Steven
admin
Site Admin
 
Posts: 2970
Joined: Mon Jun 15, 2009 9:53 am

Re: Changing a field value before save

Postby ahernandez » Fri May 18, 2018 12:49 am

Thanks Steven.

You may close the subject
ahernandez
 
Posts: 49
Joined: Thu May 03, 2018 7:38 pm

Next

Return to Custom Code