Issue with Parsing JSON content in email script

Kumar38
Kilo Sage

 

I am sending a JSON to my email script using BR and Notifications, I am unable to parse my JSON Script in email script. Please help.

Requirement: When any of the 3 fields, send a notification containing changed fields and their values

 

AFTER BUSINESS RULE with filters to validate if any of 3 fields change (choice 1,2,3):

(function executeRule(current, previous /*null when async*/) {

	var fieldNames = ["u_choice_1","u_choice_2","u_choice_3"]; // Field Names
	var notificationValues = [];
		for(var i=0; i<fieldNames.length; i++) {
			
			var eachField = {};
			eachField["oldValue"] = previous[fieldNames[i]].toString();
			eachField["newValue"] = current[fieldNames[i]].toString();
			eachField["label"] = current[fieldNames[i]].getLabel();
			notificationValues.push(eachField);
			
		}
	
	
	gs.addInfoMessage('notificationValues-----'+JSON.stringify(notificationValues));
	
	gs.eventQueue("rsp.itrm", current,JSON.stringify(notificationValues));

})(current, previous);

 

OUTPUT:

notificationValues-----[{"oldValue":"no","newValue":"no","label":"Choice 1"},{"oldValue":"yes","newValue":"no","label":"choice 2"},{"oldValue":"yes","newValue":"yes","label":"choice 3"}]

 

EMAIL SCRIPT :

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

	//[{"oldValue":"no","newValue":"yes","label":"Choice 1"},{"oldValue":"yes","newValue":"yes","label":"choice 2"},{"oldValue":"yes","newValue":"yes","label":"choice 3"}]
	
          // Add your code here

	var notiBodyString = event.parm1;
	var parser = new JSONParser();
	var parsedData = parser.parse(notiBodyString);
	template.print('<br>');
	template.print('parsedData----------'+parsedData+'\n'+'\n');
	var notiLength = parsedData.length;
	template.print('<br>');
	template.print('notiLength----------'+notiLength);
	
for(var j=0; j<notiLength; j++) {
	template.print('<br>');
	template.print('Log1');
	var eachField = parsedData[j];
	template.print('<br>');
	template.print('parsedData[j]----------'+parsedData[j]);
	template.print('<br>');
	template.print('Log2');
	if(eachField[oldValue] !== eachFiled[newValue]) {
		template.print('Log3');
	template.print("Following field :"+eachField[label]+"--Changed from---"+eachField[oldValue]+"---to---"+eachFiled[newValue]);}
}

})(current, template, email, email_action, event);

 

//I need to compare old value and new value of a field and if not equal send email

 

OUTPUT:

notiBodyString----------[{"oldValue":"Undefined","newValue":"no","label":"Choice 1"},{"oldValue":"no","newValue":"no","label":"choice 2"},{"oldValue":"yes","newValue":"yes","label":"choice 3"}]
parsedData----------[object Object],[object Object],[object Object]
notiLength----------3
Log1
parsedData[j]----------[object Object]
Log2

 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please update as below;

if(parsedData[j].oldValue != parsedData[j].newValue) {


}

no need of this line; please remove below

var eachField = parsedData[j];

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please update as below;

if(parsedData[j].oldValue != parsedData[j].newValue) {


}

no need of this line; please remove below

var eachField = parsedData[j];

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader