logo

NJP

Print updated fields in email notification with previous and current values

New article articles in ServiceNow Community · Dec 28, 2024 · article

Many a times there is a need to print updated field values in email notification wherein we require previous and current field value.

Below are the steps for the same. Please enhance it as per your requirement.

1) create after update BR on incident table

Business rule Script:

(function executeRule(current, previous /*null when async*/) { var result = getChangedFieldNames(current); gs.eventQueue('event_name', current, recipient, result); function getChangedFieldNames(gr) { var result = []; var gru = GlideScriptRecordUtil.get(current); var changedFieldNames1 = gru.getChangedFieldNames(); var changedFieldNamesarray = j2js(changedFieldNames1); var arrayUtil = new ArrayUtil(); for(var i=0;i<changedFieldNamesarray.length;i++){ // exclude fields starting with sys_ as those are OOB if(changedFieldNamesarray[i].indexOf('sys_') != -1){ var obj = {}; obj["field"] = changedFieldNamesarray[i].toString(); obj["previousValue"] = previous[element].toString(); obj["currentValue"] = current[element].toString(); result.push(obj); } } return JSON.stringify(result); } })(current, previous);

2) Create event on incident table

3) Create email notification on incident table and link the above event to this notification

4) create email script and include that in your notification as below

${mail_script:get_values}

Email Script:

(function runMailScript(current, template, email, email_action, event) { // Add your code here var jsonString = event.parm2; var parser = JSON.parse(jsonString); for(var i=0;i<parser.length;i++){ template.print('Field ' + parser[i].field + ' changed from ' + parser[i].previousValue + ' to ' + parser[i].currentValue); } })(current, template, email, email_action, event);

View original source

https://www.servicenow.com/community/service-operations-workspace/print-updated-fields-in-email-notification-with-previous-and/ta-p/3136919