Posting Attachments through Scripted REST API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 11:02 AM
We have a requirement where a third part application needs to send an attachment to Service Now and attach it to a record. We cannot use the out of box Attachment API as that requires the Sys Id of the record being updated and they won't have it. So I am building a Scripted Rest API based on the following GlideSysAttachment command.
I have tried to add this sample code from the documentation and updated the sys id of an incident that exists in my instance.
var attachment = new GlideSysAttachment();
var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';
var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);
gs.info('The attachment sys_id is: ' + agr);
When I run this through fix script to test the logic, I get the following message,
GlideTime - unparseable date: invalid function
*** Script: The attachment sys_id is: undefined
And the attachment does not get added. What am I doing wrong? Should this work through a fix script as a test?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 11:08 AM
Hi @anthonydalferro ,
Sample JSON request:
{
"incidentNumber": "INC000124",
"fileName": "my file.txt",
"contentType": "text/plain",
"fileData": "my sample file"
}
Scripted REST API Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { var requestBody = request.body.dataString; var parser = new global.JSON(); var parsedData = parser.decode(requestBody); var number = parsedData.incidentNumber; var fileName = parsedData.fileName; var fileContentType = parsedData.contentType; var fileData = parsedData.fileData; var rec = new GlideRecord('incident'); rec.addQuery('number',number); rec.query(); if(rec.next()){ var sa = new GlideSysAttachment(); sa.write(rec, fileName, fileContentType, fileData); var responseBody = {}; responseBody.incNumber = number; responseBody.status = "Success"; response.setBody(responseBody); } else{ var responseBodyFailure = {}; responseBodyFailure.status = "Failure"; response.setBody(responseBodyFailure); } })(request, response);
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful based on the Impact.
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2024 04:03 AM
I need to use base64 as that is the data I will be receiving.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2024 12:14 PM
@anthonydalferro Please check below and see if helpful.
https://www.servicenow.com/community/developer-forum/sending-attachment-using-rest-api/m-p/1786194
Please mark my answer correct and helpful if this works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2024 04:56 AM
That is for sending attachments through a business rule out of ServiceNow. I need to accept an attachment.