- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 05:05 PM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 06:18 PM
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));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 07:02 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 07:08 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 07:19 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2020 05:51 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 07:08 PM
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));