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

ajayr
Giga Expert

Hi Ali,

 

I did the same thing but still getting this error

 

 

find_real_file.png

 

are you wring mail_script in notification itself or writing script in"Notification email scripts" and calling in notification? 

 

also please have a log statement before json.parse to see what value is coming in event.parm1.

 

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

I am writing in notification email script and using that in my Notification.

I am receiving the value in parm1 as below

find_real_file.png  One strange thing, the order is not correct when I am receiving via event.parm1 in email script.

while as on sending from business rule , the format and order seems correct as in the above log starting with "kt5hlbg all ciDataValues"

Hi 

It seems you are doing the wrong formatting of JSON. Try formatting properly.

Hi Omkar,

 

Can you be more specific about the wrong formatting of JSON