Passing JSON object to email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 01:20 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 01:21 AM
Hi
Follow this link ;-
Just the requirement you need.
Mark correct if it helps.
Regards,
Omkar Mone
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 01:24 AM
Hi Omkar,
Thanks, but I have already gone through that post and I think I followed the same. Still no luck

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 01:29 AM
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
Thank you,
Ali

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2018 01:36 AM
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
Thank you,
Ali