How to include attachment for the record generated from the Json response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 08:09 AM
Hi all,
We have an integration already in place for a scoped application, when response is generated a scoped application record is created. We have a requirement to include the attachments that are coming from the json response.
Below is the json response code we are receiving from third party tool.
Please help me the script/syntax which need to be added to include the attachments.
"dataDocuments":[
"attachmentType":"Relieving Letter"
},
{
"attachmentType":"Hike Letter"
},
{
"attachmentType":"Offer Letter"
},
]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 08:42 AM - edited 01-10-2024 09:11 AM
To clarify:
You have an integration with a 3rd party tool that claims to be sending files in its API Response body?
If that is the case, and your example is the entire response with no information redacted; Nothing in that response includes any files, because JSON has to be a string. So to send a file, the response would either need to include a path to where the file lives on the server or the file would need to be encoded as a string.
If the response does include encoded strings and you've just redacted them (which is understandable because it would exceed your post limit) then you can run the following to decode the data and save it as an attachment:
function addAttachment(string) {
// Decode the base64 string into a byte array
var baseData = gs.base64Decode(string);
// Create an attachment record
var attachment = new GlideSysAttachment();
attachment.setFileName("file.format"); // Set the file name for the attachment
attachment.setTableName(/* Table name of new record*/);
attachment.setTableSysId(/* sys_id of record you need to attach the files to*/);
// Attach the decoded file to the attachment record
attachment.writeBase64Content(baseData);
// Insert the attachment record
attachment.insert();
}
var jsonBody = body; //update this to how you are currently storing the JSON body.
var jsonObject = JSON.parse(jsonBody);
for(var key in jsonObject) {
if(key == "encoded") {
addAttachment(jsonObject[key]);
}
}
/*
The Above for loop will need to be adjusted based on how the response is formatted.
"encoded" will need to be adjusted to the name of the key that stores the encoded value as it's property.
*/