- 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 05:34 PM
Hi,
There isn't a "JSON file"...but a certain file type that can contain a JSON payload perhaps.
Do you have more specifics on that?
For example, these are some file types:
docx: application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc: application/msword
jpg: image/jpeg
png: image/png
xlsx: application/xlsx OR application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
xls: application/vnd.ms-excel
pptx: application/vnd.openxmlformats-officedocument.presentationml.presentation
pdf: application/pdf
Please mark reply as Helpful/Correct, if applicable. Thanks!
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 05:49 PM
Hi Allen,
right, I could create a ".txt" file that contains a JSON payload, correct? If so, how do I populate the body of the file, am I on the right track with the GlideRecord? Again, what I'm looking to do is to have the file attach to the RITM and have file contain the JSON info (which will be all the variable names and values).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 06:02 PM
Yea and actually, I think there is a JSON file...extension...but I personally have not dealt with that. In my experience, it's just data passed through REST, etc. so I wanted to reverse my prior statement on that to say that there may actually be a JSON file type.
Normally I've seen it as:
application/json, but in terms of a file name, I haven't had experience with test.json as a file name, for example.
But...to create it in Word doc for example...
You would need to script it out like so:
var myString = "Test 1 2 3 4 5";
var gr = new GlideRecord('incident');
gr.get('sys_id_here');
var attach = new GlideSysAttachment();
attach.write(gr, 'test.doc', 'application/msword', myString);
So this is an example and I think you can take it from here, but you would query for the record you want to attach it to, or use a simple "get" like I did to go right to it, if needed.
Then you'd want to build out your JSON object with all the data:
So using the simplest form for an example:
{"name one":"value one","name two":"value two"}
You could do something like:
{"name one":gr.variables.field1,"name two":gr.variables.field2}
And so on.
So in the script I provided above, you would substitute your JSON object for the "myString" variable.
I think you can take it from here?
Please mark reply as Helpful/Correct, if applicable. Thanks!
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 06:04 PM
excellent, thanks Allan, let me give that a try.