- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 04:03 AM
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!!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 04:26 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 04:26 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 04:36 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-05-2022 08:08 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 04:45 AM
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?