Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Hardware Assets API

JimFaraone
Tera Contributor

Hello All

I'm trying to collect information about all changes made to a hardware asset.  I am querying to sys_audit table but I cannot seem to find that change below.   What table do I look at to find this history   sys_history_line maybe?

 current URL:  https://<domian>.servicenowservices.com/api/now/table/sys_audit


JimFaraone_0-1739993708415.png

Thank you

 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

You don't want to query sys_audit, or any of the temporary audit tables such as sys_history_set or sys_history line.  Instead create your own audit table (u_audit), with the same fields of sys_audit. Then use a Business rule to tack changes in the new table.

 

Script logic for the BR would look like:

 

(function executeRule(current, previous /*null when async*/) {

	// create audit history.
	var fieldName = "";
	var newValue = "";
	var oldValue = "";
//	gs.info("PAN Audit Incident: " + current.number + ", Field: " + current.fieldname + ", Old: " + current.oldvalue + ", New: " + sarec.newvalue + ".");
	if (current.assigned_to != previous.assigned_to) {
		fieldName = 'assigned_to';
		newValue = current.assigned_to;
		oldValue = previous.assigned_to;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.assignment_group != previous.assignment_group) {
		fieldName = 'assignment_group';
		newValue = current.assignment_group;
		oldValue = previous.assignment_group;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.priority != previous.priority) {
		fieldName = 'priority';
		newValue = current.priority;
		oldValue = previous.priority;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.state != previous.state) {
		fieldName = 'state';
		newValue = current.state;
		oldValue = previous.state;
		createRecord(current, fieldName, newValue, oldValue);
	}


	function createRecord(curRec, recFieldName, recNewValue, recOldValue) {
		// Create audit record
		var panAudit = new GlideRecord('u_pan_audit');
		panAudit.initialize();
		panAudit.u_documentkey = curRec.sys_id;
		panAudit.u_fieldname = recFieldName;
		panAudit.u_newvalue = recNewValue;
		panAudit.u_oldvalue = recOldValue;
		panAudit.u_tablename = 'incident';
		panAudit.u_updated_on = curRec.sys_updated_on;
		panAudit.u_user = curRec.sys_updated_by;
		if (!panAudit.insert()) {
			gs.info("PAN Audit Incident: Error creating audit, Table: " + panAudit.u_tablename + ", Field: " + panAudit.u_fieldname + ", Old: " + panAudit.u_oldvalue + ", New: " + panAudit.u_newvalue + ".");
		}
	}

})(current, previous);

Add "if () {" blocks for each field you're interested in. (The above audit table is named 'u_pan_audit').

View solution in original post

1 REPLY 1

Bert_c1
Kilo Patron

You don't want to query sys_audit, or any of the temporary audit tables such as sys_history_set or sys_history line.  Instead create your own audit table (u_audit), with the same fields of sys_audit. Then use a Business rule to tack changes in the new table.

 

Script logic for the BR would look like:

 

(function executeRule(current, previous /*null when async*/) {

	// create audit history.
	var fieldName = "";
	var newValue = "";
	var oldValue = "";
//	gs.info("PAN Audit Incident: " + current.number + ", Field: " + current.fieldname + ", Old: " + current.oldvalue + ", New: " + sarec.newvalue + ".");
	if (current.assigned_to != previous.assigned_to) {
		fieldName = 'assigned_to';
		newValue = current.assigned_to;
		oldValue = previous.assigned_to;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.assignment_group != previous.assignment_group) {
		fieldName = 'assignment_group';
		newValue = current.assignment_group;
		oldValue = previous.assignment_group;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.priority != previous.priority) {
		fieldName = 'priority';
		newValue = current.priority;
		oldValue = previous.priority;
		createRecord(current, fieldName, newValue, oldValue);
	}
	if (current.state != previous.state) {
		fieldName = 'state';
		newValue = current.state;
		oldValue = previous.state;
		createRecord(current, fieldName, newValue, oldValue);
	}


	function createRecord(curRec, recFieldName, recNewValue, recOldValue) {
		// Create audit record
		var panAudit = new GlideRecord('u_pan_audit');
		panAudit.initialize();
		panAudit.u_documentkey = curRec.sys_id;
		panAudit.u_fieldname = recFieldName;
		panAudit.u_newvalue = recNewValue;
		panAudit.u_oldvalue = recOldValue;
		panAudit.u_tablename = 'incident';
		panAudit.u_updated_on = curRec.sys_updated_on;
		panAudit.u_user = curRec.sys_updated_by;
		if (!panAudit.insert()) {
			gs.info("PAN Audit Incident: Error creating audit, Table: " + panAudit.u_tablename + ", Field: " + panAudit.u_fieldname + ", Old: " + panAudit.u_oldvalue + ", New: " + panAudit.u_newvalue + ".");
		}
	}

})(current, previous);

Add "if () {" blocks for each field you're interested in. (The above audit table is named 'u_pan_audit').