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

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.


Hi Chuck,



Thanks for your answer. Let me try it. Just to make sure, I understood you correct, I would need to do a JSON.stringify() after JSON.encode() to be able to use JSON.parse() in the email script. Is it correct?



Darshak


JSON.stringify() is the ECMAscript 5 method for turning a Javascript object and turning it in to a string. It replaces the new JSON().encode() method. You need to be on Helsinki or higher to use it, otherwise stick with new JSON().encode() and decode().



JSON.parse() is the ECMAscript 5 version of new JSON().decode().


Thanks. I'm running this on my personal instance which is on Jakarta. So I believe, I'm good to test it. Thank you. I'll get back to you after I test it.



Darshak