Script include function to check old vs new value on form

Robin30
Tera Contributor

Hello everyone,

I have a requirement to code a function in a Script Include to be able to check what has been changed on a form (only custom added fields).

Every solution I've tried so far also prints info like "sys_updated_on" and some other solutions wont work because it's an Scope app.

Can anyone give an simple example of this kind of Function?

Basically i need to check which fields have been changed and print the old/new values into a "Change" log field on my form.

I already got an existing Client script that i will modify to populate the "Change Log" field, but Im not sure how to compare old/new values on the form with a Script include function.

Do I send sys_id of that specific record and then make the comparison for every specific field?

 

 

Thanks!!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use after update business rule and use this script to check which fields got changed and print the old and new values

this link has the solution to know the field names; you can skip the system fields and then use current and previous object to print the old and new values

How to get Changed or Modified Fields in ServiceNow Scoped Apps?

sample BR script here

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

	// Add your code here
	//this will give you array of changed field names
	var changed_fields = getChangedFieldNames(current);

	for(var i in changed_fields)
		gs.addInfoMessage("field" + changed_fields[i] + " old value " + previous[changed_fields[i]] + " new value " + current[changed_fields[i]]);

	function getChangedFieldNames(gr) {

		var result = [];
		var elements = gr.getElements();
		var size = elements.length;
		for (var i = 0; i  < size; i++) {
			var ge = elements[i];
			if (ge.changes() && !ge.getName().startsWith('sys')) { // skip the system fields while pushing into array
				result.push(ge.getName());
			}
		}

		return result;
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use after update business rule and use this script to check which fields got changed and print the old and new values

this link has the solution to know the field names; you can skip the system fields and then use current and previous object to print the old and new values

How to get Changed or Modified Fields in ServiceNow Scoped Apps?

sample BR script here

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

	// Add your code here
	//this will give you array of changed field names
	var changed_fields = getChangedFieldNames(current);

	for(var i in changed_fields)
		gs.addInfoMessage("field" + changed_fields[i] + " old value " + previous[changed_fields[i]] + " new value " + current[changed_fields[i]]);

	function getChangedFieldNames(gr) {

		var result = [];
		var elements = gr.getElements();
		var size = elements.length;
		for (var i = 0; i  < size; i++) {
			var ge = elements[i];
			if (ge.changes() && !ge.getName().startsWith('sys')) { // skip the system fields while pushing into array
				result.push(ge.getName());
			}
		}

		return result;
	}

})(current, previous);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hello Ankur,

Thanks for the quick answer, I will try this solution.

But just to be sure, you mean I should use this BR and then call this function/return these values to my Client Script to populate?

 

 

Thanks

Hi,

nope

you should be using this script inside before update business rule and then populate the changed field in the "changed field values" field

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Robin,

Is the question about having a form.

By "change", does this imply change on the currently displayed form with what's already saved in the database table or is the form suppose to compare specified table entries of records in other tables?

How will the end-user process the form? For example, the user (1) opens a new form, (2) enters a value. What should be displayed in "Change Log" field? Will user submit the form?