Is there a possibility to pass a JSON object to an event parameter?

Rama Chandra D
Kilo Guru

Hello,

I'll be glad if anyone can clarify, if an array of JSON objects can be passed as an event parameter and be retrieved as a JSON object rather than a string in either notifications or email scripts? If it isn't possible, is there any other way? Currently, I'm working to build an email notification - any edit on a particular table and only those fields that were created by a user (and not system fields), should trigger a notification. I was successfully able to get the fields, the previous and the current field values in a BR. However, when I encoded this information in a JSON object and passed it to an event, I'm unable to retrieve is as a JSON but rather it getting converted into a string array. Probably, because the event parameters are of type 'string'.

The JSON.parse() works fine in the email script as long as there is only one object, but when I have multiple objects (one for each field change), the parse() fails. If my understanding is correct, it is unable to process ',' character in the JSON object array. Any help or any better way to do this will be appreciated.

Thank you.

Best,

Darshak

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Darshak,



You cannot pass a native object as you discovered. It must be a string. As a result, you need to use JSON.stringify() to create the string and JSON.parse() to convert the string back to an object.



Have you tried with a very simple use case like this:



// Assumes current is a GlideRecord object


var jsonStr = '[{'name' : 'one'}, {'name' : 'two'}, {'name' : 'three'}]';


gs.eventQueue('mytable.some_event', current, jsonStr, '');



Then in your email script:



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


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


        template.print(i + '=' + ' -- name = ' + myList[i].name);


}



I'd start out with a very simple test something like that and see if you can get it to work before moving on. Warning - none of the above code was tested.


View solution in original post

8 REPLIES 8

Hey Chuck,



I did test the lines of code provided by you, however, I need to changed it this way,



var jsonStr = [{'name' : 'one'}, {'name' : 'two'}, {'name' : 'three'}];


  var test = JSON.stringify(jsonStr);


  gs.eventQueue('needIt.form.updated', current, test, '');



For some reason, having quotes around the array was throwing me a warning. The notification works as I was hoping.' Now, I need to try it on my script.



Thanks


Thanks Chuck. I was able to implement what I wished for.



Best,


Darshak


Glad you got it working.


ajayr
Giga Expert

Hi Chuck/Rama,

 

I tried the same thing in my case but some how I am getting this error 

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

 

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