Welcome to the nuBuilder Forums!

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

Variables to be used between different moment in a form

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Variables to be used between different moment in a form

Unread post by yvesf »

Hello,


At the form level, I use nuBeforeSave() and nuAfterSave() functions. I would like to retrieve the value of a field before saving and after saving.
Where should I put my variables oldlbl and newlbl ? at the form level or at the setup header section ? what is the recommended approach ?

Code: Select all

function nuBeforeSave() {
let oldlbl=nuGetValue('tsk_label');
}


function nuAfterSave(){
let newlbl=nuGetValue('tsk_label'); // we retrieve the new value
 if (newlbl!=oldlbl) ...  // I would like to retrieve oldlbl set in the other function nuBeforeSave()
}
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Variables to be used between different moment in a form

Unread post by kev1n »

Use nuSetProperty() and nuGetProperty(). But isn't oldlbl and newlbl the same anyway or are you going to change it by PHP while saving?
yvesf
Posts: 315
Joined: Sun Mar 14, 2021 8:48 am
Location: Geneva
Has thanked: 87 times
Been thanked: 11 times

Re: Variables to be used between different moment in a form

Unread post by yvesf »

In fact I use nuOnLoad function at the form level and it works if I declare my variables in the setup header.
My objective was to record all modifications done in a specific field and it works. Not very academic as nuOnLoad function should not be there. Any example of audit trail visible for the end user ?
I have built my solution using console.log. Any other mean to follow values in a variable ? A tool ?
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Variables to be used between different moment in a form

Unread post by kev1n »

The bests place to capture changes is the BS (Before Save) PHP event. In theory, retrieve the previous value from the table, compare it with the current value and if it's different, write the previous value into an audit table.
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: Variables to be used between different moment in a form

Unread post by kev1n »

Here's an example how to track changes for one field using an audit log table.

A table audit_log must be created:

Code: Select all

CREATE TABLE `audit_log` (
  `audit_log_id` varchar(25)  NOT NULL,
  `aud_column` varchar(50)  DEFAULT NULL,
  `aud_old_value` varchar(1000)  DEFAULT NULL,
  `aud_new_value` varchar(1000)  DEFAULT NULL,
  `aud_user_id` varchar(25)  DEFAULT NULL,
  `aud_changed_at` datetime DEFAULT current_timestamp()
) ;

ALTER TABLE `audit_log` ADD PRIMARY KEY (`audit_log_id`);

Code in BS:

Code: Select all

$table = 'your_table'; // table name
$pk = 'your_table_id'; // primary key
$recordId = '#RECORD_ID#'; // current record id
$columnName = 'object_id'; // db column name, e.g. cus_name

$oldValue = db_fetch_value($table, $pk, $recordId, $columnName); // function added to nuBuilder on Oct 19, 2022

$newValue = '#object_id#'; // current value of the object "object id", e.g. cus_name

// If old and new value don't match
if ($oldValue != $newValue) {

$insert = "
              INSERT INTO audit_log (aud_column, aud_old_value, aud_new_value, aud_user_id )
              VALUES (?, ?, ?, ?)

              ";

nuRunQuery($insert, [$columnName, $oldValue, $newValue, '#USER_ID#']);

}
Beware, I just typed it here, this code is not tested!
nc07
Posts: 118
Joined: Tue Jun 04, 2019 4:05 am
Has thanked: 5 times
Been thanked: 22 times

Re: Variables to be used between different moment in a form

Unread post by nc07 »

This might also be of some help too: viewtopic.php?f=20&t=9413
Post Reply