Passing JSON object to email script

ajayr
Giga Expert

Hi,

 

My requirement is to send Email notification to Service owner whenever there is any change/update in CI fields.

The notification should contain "Name of Field", "Previous Value" and "New Value"

In order to achieve that I am using JSON to pass those field/values from BR to email_script as below:

 

Business Rule (on cmdb_ci and running after update):

var recipients = current.owned_by.toString();

var gru = GlideScriptRecordUtil.get(current);
var fields = gru.getChangedFieldNames(); //Get changed fields with database names
gs.include('j2js');
fields = j2js(fields);
var jsonObj = [];
var ciData;
for(var i in fields){

var field = fields[i];
ciData = {
"FieldLabel":current[field].getLabel(),
"FieldValue":current[field].getDisplayValue(),
"FieldPrevValue":previous[field].getDisplayValue()
};

jsonObj.push(ciData);

}

var jsonStr = JSON.stringify(jsonObj);

new AZNotificationI18NUtils().eventQueue('az.cmdb.updateCI', current, jsonStr, recipients);
}

 

//Here I am getting the output of jsonStr as below:

[{"FieldLabel":"Domain Name","FieldValue":"test2","FieldPrevValue":"test"},{"FieldLabel":"Provider tag","FieldValue":"BNL ServerList","FieldPrevValue":"Allianz Benelux"}]

 

Email script as below:

------------------------

var myList = JSON.parse(event.parm1);

for (var i = 0; i < myList.length; i++) {

gs.log("Result"+myList[i].FieldLabel);
}

--------------------------------

But I am getting error on line "var myList = JSON.parse(event.parm1);" as 

"org.mozilla.javascript.EcmaError: Expected end of stream at char 131"

 

Could you please check if I am missing here something

 

9 REPLIES 9

Omkar Mone
Mega Sage

Hi 

Follow this link ;- 

https://community.servicenow.com/community?id=community_question&sys_id=472cc365db9cdbc01dcaf3231f96...

 

Just the requirement you need.

 

Mark correct if it helps.

 

Regards,

Omkar Mone

www.dxsherpa.com

Hi Omkar,

 

Thanks, but I have already gone through that post and I think I followed the same. Still no luck

Ahmmed Ali
Mega Sage

Hello,

 

JSON object is in the form of key and value pairs. here you are using array in place of key and value pairs.

 

you can modify the script as below to use the same array.

 

in BR, in place of var jsonStr = JSON.stringify(jsonObj);

 

use: var jsonObject = {"valueArray":jsonObj};

var jsonStr = JSON.stringify(jsonObject);

 

in mail script: in place of var myList = JSON.parse(event.parm1);

 

use: var myListObject = JSON.parse(event.parm1);

var myList = myListObject.valueArray;

 

let me know if any issue.

 

Thanks,

Ali

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

One correction.

 

Hello,

 

JSON object is in the form of key and value pairs. here you are using array in place of key and value pairs.

 

you can modify the script as below to use the same array.

 

in BR, in place of var jsonStr = JSON.stringify(jsonObj);

 

use: var jsonObject = {};

jsonObject.jArray=jsonObj; //pass array as a value and later you can read it after parsing the same

var jsonStr = JSON.stringify(jsonObject);

 

in mail script: in place of var myList = JSON.parse(event.parm1);

 

use: var myListObject = JSON.parse(event.parm1);

var myList = myListObject.valueArray;

 

let me know if any issue.

 

Thanks,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali