
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 01:04 PM
The short story is - I have a need to know which fields are changing in the "current" object that's available in a Business Rule. I've seen a post on ServiceNowGuru about this but it involves J2JS, which isn't available in non-global application scopes.
If it helps, the use case is basically that there needs to be a separate change log of things that have happened to the record, and I don't want to hard code a list of fields to check for changes when this Business Rule runs.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 02:24 PM
Don't actually think you need to go get the fields, since they already is in the current/previous objects.
Something like this should work:
(function executeRule(current, previous /*null when async*/) {
for (var x in current){
if (current[x] != previous[x]) {
gs.addInfoMessage ('Field ' + x + ' has changed!' );
}
}
})(current, previous);
//Göran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 02:01 PM
Hi Patrick,
I would get the list of field by getting the dictionary table record and loop thru each fields to compare each current.field with previous.field values.
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord('sys_dictionary');
gr.addQuery('name=incident^ORname=task^element!=NULL'); // Replace 'incident' by the table that is relevant to you
gr.query();
while (gr.next()) {
if (current[gr.element] != previous[gr.element] ) {
gs.addInfoMessage ('Field ' + gr['name'] + '.' + gr.element + ' has changed!' ); // Insert your here code to save log where you want
}
}
})(current, previous);
Is that enough for you to start?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 02:24 PM
Don't actually think you need to go get the fields, since they already is in the current/previous objects.
Something like this should work:
(function executeRule(current, previous /*null when async*/) {
for (var x in current){
if (current[x] != previous[x]) {
gs.addInfoMessage ('Field ' + x + ' has changed!' );
}
}
})(current, previous);
//Göran
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 02:30 PM
Thanks Goran.. ran the script its very functional..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2017 02:32 PM
And depending on how you want it, this might be enough for you as well:
(function executeRule(current, previous /*null when async*/) {
var gru = GlideScriptRecordUtil.get(current);
gs.addErrorMessage(gru.getChangedFields());
})(current, previous);