Page 1 of 1
Variables to be used between different moment in a form
Posted: Tue Nov 08, 2022 8:11 pm
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()
}
Re: Variables to be used between different moment in a form
Posted: Tue Nov 08, 2022 8:19 pm
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?
Re: Variables to be used between different moment in a form
Posted: Tue Nov 08, 2022 10:11 pm
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 ?
Re: Variables to be used between different moment in a form
Posted: Wed Nov 09, 2022 7:32 am
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.
Re: Variables to be used between different moment in a form
Posted: Wed Nov 09, 2022 1:27 pm
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!
Re: Variables to be used between different moment in a form
Posted: Thu Nov 10, 2022 12:50 am
by nc07
This might also be of some help too:
viewtopic.php?f=20&t=9413