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

Hi,

Yea, see my last reply and the bold part.

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.


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

And the JSON can be built either way that we both mentioned and again...I just used the example from the ServiceNow documentation, so it was meant as just a sample to get things going.

But yes, for longer objects and such, it's far easier to just declare the object, then just populate it line by line. It's more of a preference, but either way would work.


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

Hmm...not sure I'm following that part...isn't "gr.getValue('field_name')" referring to a column on the table?  I need the value in the variable field.

 

 

Patrick,

When you said:

"Hmm...not sure I'm following that part...isn't "gr.getValue('field_name')" referring to a column on the table?  I need the value in the variable field."

It's an example dude...

When I respond, just as an fyi, it's to give you an example and to help you learn and go from there (notice in reply marked Correct above I say ...twice actually... "you can take it from here?"...)...

 

 

 


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

I had same issue with it so I tried below and worked for me. (Same code as previous post)

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