Page 1 of 2
Changing a field value before save
Posted: Mon May 14, 2018 8:43 pm
by ahernandez
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.
Re: Changing a field value before save
Posted: Tue May 15, 2018 6:37 am
by toms
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.
Re: Changing a field value before save
Posted: Tue May 15, 2018 8:13 am
by ahernandez
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...
Re: Changing a field value before save
Posted: Tue May 15, 2018 8:15 am
by ahernandez
Sorry toms,
I am doing this in BS section, not in BB. It was a mistake.
Re: Changing a field value before save
Posted: Tue May 15, 2018 11:17 pm
by admin
ahernandez,
I would let nuBuilder save the record first and then update the record(s) you want in After Save with nuRunQuery().
Steven
Re: Changing a field value before save
Posted: Wed May 16, 2018 6:18 am
by ahernandez
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.
Re: Changing a field value before save
Posted: Wed May 16, 2018 6:20 am
by ahernandez
By the way...
How to control if nuRunQuery has failed updating or inserting records?
Re: Changing a field value before save
Posted: Wed May 16, 2018 7:59 am
by toms
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;
}
Re: Changing a field value before save
Posted: Thu May 17, 2018 12:55 am
by admin
ahernandez,
SQL errors will show up in Debug Results.
debug.png
Steven
Re: Changing a field value before save
Posted: Thu May 17, 2018 5:19 pm
by ahernandez
Thanks Steven.
You may close the subject