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!