- 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:53 AM
Hi!
Good questions haha.
It should work like this:
Users enters a already existing record.
The user changes some values in some fields ---> Submit the record again.
When the user has submitted the record, "changed field values" should be populated in the "Change Log" field.
So I guess this should be possible with only a BR