Ankur Bawiskar
Tera Patron
Tera Patron

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

 

Comments
Rafał Rataj
Tera Contributor

Thank You for sharing! 🙌

SmitaH
Tera Contributor

Hello Ankur, this script didn't work for me, could you please guide more on this part.

 

Thanks

Regards, 

Smita

Version history
Last update:
‎12-28-2024 02:23 AM
Updated by:
Contributors