Is it possible to create and attach a JSON file to a RITM in server side scripting?

patricklatella
Mega Sage

Hi all,

I've got a requirement to automatically create and attach a JSON file to a RITM when a specific catalog item is submitted.  The JSON file needs to have the variables and their values.  Is this possible?  I'm thinking maybe it can be done with GlideRecord to create the attachment and attach to the RITM, but not sure how to populate the JSON file with the variable names and values.  Any thoughts?  thanks!

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Do you have lot of variables that you want to add? If you have few then you can manually push them and save it as Allen recommended.

If you are doing this in run script then you can do something like

var payload = {};

payload.number = current.getValue('number');

payload.short_description = current.getValue('short_description');

var attachment = new GlideSysAttachment();
attachment.write(current, 'payload.json', 'text/plain', JSON.stringify(payload));

View solution in original post

22 REPLIES 22

yep, that works Allen...I'll pay with it to get exactly what I need.  Thanks!

Mike Patel
Tera Sage

Do you have lot of variables that you want to add? If you have few then you can manually push them and save it as Allen recommended.

If you are doing this in run script then you can do something like

var payload = {};

payload.number = current.getValue('number');

payload.short_description = current.getValue('short_description');

var attachment = new GlideSysAttachment();
attachment.write(current, 'payload.json', 'text/plain', JSON.stringify(payload));

Indeed. Great advice Mike.

Patrick, he's referring to how the object is being built.

The new modern way of creating an object is with something like:

var payload = {};

So, then it's easier to populate it with object.field = queryvariable.field...

I was just using the example from the documentation I linked.

Also...notice the "gr.getValue('field_name')" and "gr.getDisplayValue('field_name'); instead of just gr.number or gr.category. As you build the object.

However, one extra piece I did not mention was that you need to JSON.stringify your object for it to show up properly in your newly created document.

Otherwise it'll show up as [Object object]...so that piece I did forget to mention.

But in my example...I used a string variable so that would show up correctly without JSON.stringify.

Thanks Mike for bringing that up.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Mike,

thanks for the tip!...I'll see if I can encorporate your suggestion.  

patricklatella
Mega Sage

thanks guys for the help, definitely have something I can work with here.

 

Regarding the 

var payload = {}; 

with Mike's first example, which I turned into this (again I need my pairs to be the variable name and the value entered when the catalog item was submitted)

 

var payload = {};

payload.description = current.variables.description;
payload.requested_for_2 = current.variables.requested_for_2;

var attachment = new GlideSysAttachment();
attachment.write(current, 'payload.txt', 'text/plain', JSON.stringify(payload));

 

my payload in the attachment looked like this:

{"description":{},"requested_for_2":{}}

 

so it appears to be missing the actual values entered for the variables when the form was submitted.  Here's a screen shot of the variables section on the RITM to show the "Description" variable.  Note the text entered is missing from the JSON.  I think I could build the JSON with Allen's first suggestion, but is there a better way using Mike's suggestion?

 

find_real_file.png