Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Is there a way to detect which fields are being changed from a Business Rule?

Patrick Schult2
Giga Guru

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.

1 ACCEPTED SOLUTION

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


View solution in original post

8 REPLIES 8

Shiva Thomas
Kilo Sage

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);



Capture d



Is that enough for you to start?


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


Thanks Goran.. ran the script its very functional..


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);