Insert Info from Updated New Record into an Audit Trail?
Posted: Tue Sep 14, 2021 11:02 am
Hello,
I've been using the some modified audit trail code from this post.
viewtopic.php?f=20&t=9413
It is used in Before Save, so that any changes made to a record are logged.
For new records I've set it so that the -1 value generated by a new record is instead displayed as "New" in another column. For editing of existing records the lot number is inserted instead.
This all works well so far...
However, the lot number assigned to the records is created by code in the After Save section (by taking an autonumber and prepending a prefix to it, as shown below), so this information is not logged.
Ideally like to insert the result of this update event into the audit trail too, so that the new lot number is logged in the audit trail. The code would only ever be run once on records creation.
I've tried the following code but neither are working.
and
Can anyone help point me in the right direction?
Thanks,
Paul
I've been using the some modified audit trail code from this post.
viewtopic.php?f=20&t=9413
It is used in Before Save, so that any changes made to a record are logged.
Code: Select all
$nso = nuSubformObject('');
for ($i = 0; $i < count($nso->rows); $i++) {
$p = $nso->rows[$i][0];
$s = "SELECT * FROM $nso->table WHERE $nso->primary_key = '$p'";
$t = nuRunQuery($s);
$dfa = db_fetch_array($t);
$Flds = db_field_names($nso->table);
for ($I = 1; $I < count($nso->rows[$i]); $I++) {
$ID = nuID();
$Tbl = $nso->table;
$Fld = $nso->fields[$I];
$PK = $p;
$Usr = "#USER_ID#";
$Usr_name = "#USER_NAME#";
$usr_name = empty($Usr_name) ? "System Admin" : $Usr_name;
$lot = "#mpd_lot_number#";
$nsoV = $dfa[$nso->fields[$I]];
$NV = $nso->rows[$i][$I];
if ($PK == "-1") {
if ($nsoV != $NV && in_array($Fld, $Flds)) {
$S = "INSERT INTO lotgen_audit_trail (lotgen_audit_trail_id, table_name, record_identifier, field_name, pk_value, userid, user_name, old_value, new_value)"
."VALUES ('$ID', '$Tbl', 'New','$Fld', '$PK', '$Usr', '$usr_name', '$nsoV', '$NV')";
nuRunQuery($S);
}
}
else {
if ($nsoV != $NV && in_array($Fld, $Flds)) {
$S = "INSERT INTO lotgen_audit_trail (lotgen_audit_trail_id, table_name, record_identifier, field_name, pk_value, userid, user_name, old_value, new_value)"
."VALUES ('$ID', '$Tbl', '$lot','$Fld', '$PK', '$Usr', '$usr_name', '$nsoV', '$NV')";
nuRunQuery($S);
}
}
}
}
This all works well so far...
However, the lot number assigned to the records is created by code in the After Save section (by taking an autonumber and prepending a prefix to it, as shown below), so this information is not logged.
Code: Select all
$mpdlotnumber = "
UPDATE mvt_prepared_diluent
SET mpd_lot_number = CONCAT('MSL-',LPAD(mpd_autonumber,4,'0'))
WHERE mpd_lot_number IS NULL AND mvt_prepared_diluent_id = ?
";
nuRunQuery($mpdlotnumber, ["#RECORD_ID#"]);
I've tried the following code but neither are working.
Code: Select all
$ID = nuID();
$Tbl = "#table#";
$Usr = "#USER_ID#";
$Usr_name = "#USER_NAME#";
$mpdlotnumber = "
UPDATE mvt_prepared_diluent
SET mpd_lot_number = CONCAT('MSL-',LPAD(mpd_autonumber,4,'0'))
INSERT INTO lotgen_audit_trail (lotgen_audit_trail_id, table_name, record_identifier, field_name, pk_value, userid, user_name, old_value, new_value)
VALUES ('$ID', '$Tbl', 'New Lot','mpd_lot_number', '$PK', '$Usr', '$usr_name', 'N/A', '#mpd_lot_number#')
WHERE mpd_lot_number IS NULL AND mvt_prepared_diluent_id = ?
";
nuRunQuery($mpdlotnumber, ["#RECORD_ID#"]);
nuDebug(nuHash());
Code: Select all
$mpdlotnumber = "
UPDATE mvt_prepared_diluent
SET mpd_lot_number = CONCAT('MSL-',LPAD(mpd_autonumber,4,'0'))
WHERE mpd_lot_number IS NULL AND mvt_prepared_diluent_id = ?
";
nuRunQuery($mpdlotnumber, ["#RECORD_ID#"]);
$nso = nuSubformObject('');
for ($i = 0; $i < count($nso->rows); $i++) {
$p = $nso->rows[$i][0];
$s = "SELECT * FROM $nso->table WHERE $nso->primary_key = '$p'";
$t = nuRunQuery($s);
$dfa = db_fetch_array($t);
$Flds = db_field_names($nso->table);
for ($I = 1; $I < count($nso->rows[$i]); $I++) {
$ID = nuID();
$Tbl = $nso->table;
$Fld = $nso->fields[$I];
$PK = $p;
$Usr = "#USER_ID#";
$Usr_name = "#USER_NAME#";
$usr_name = empty($Usr_name) ? "System Admin" : $Usr_name;
$lot = "#mpd_lot_number#";
if ($nsoV != $NV && in_array($Fld, $Flds)) {
$S = "INSERT INTO lotgen_audit_trail (lotgen_audit_trail_id, table_name, record_identifier, field_name, pk_value, userid, user_name, old_value, new_value)"
."VALUES ('$ID', '$Tbl', 'New','$Fld', '$PK', '$Usr', '$usr_name', 'N/A', '$lot')";
nuRunQuery($S);
}
}
}
nuRunQuery($nso, ["#RECORD_ID#"]);
Thanks,
Paul