Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Posting Attachments through Scripted REST API

anthonydalferro
Tera Guru

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.

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSysA...

 

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?

5 REPLIES 5

Nikolay Megdano
Tera Expert

Maybe you are running it in global scope. This method doesn't seem to be supported in global. Copy/paste it in background script, pick any other scope and run it.