Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Changing a field value before save

Questions related to customising nuBuilder Forte with JavaScript or PHP.
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Changing a field value before save

Unread post 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.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Changing a field value before save

Unread post 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.
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Re: Changing a field value before save

Unread post 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...
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Re: Changing a field value before save

Unread post by ahernandez »

Sorry toms,

I am doing this in BS section, not in BB. It was a mistake.
admin
Site Admin
Posts: 2822
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: Changing a field value before save

Unread post 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
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Re: Changing a field value before save

Unread post 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.
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Re: Changing a field value before save

Unread post by ahernandez »

By the way...

How to control if nuRunQuery has failed updating or inserting records?
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: Changing a field value before save

Unread post 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;
        }
admin
Site Admin
Posts: 2822
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 29 times

Re: Changing a field value before save

Unread post by admin »

ahernandez,

SQL errors will show up in Debug Results.
debug.png
Steven
You do not have the required permissions to view the files attached to this post.
ahernandez
Posts: 49
Joined: Thu May 03, 2018 12:08 pm

Re: Changing a field value before save

Unread post by ahernandez »

Thanks Steven.

You may close the subject
Locked