Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Saving a RESTMessageV2 response as an attachment and getting the attachment's sys_id #ServiceNowSnippet
// This is where we'll save the attachment
var tablename = 'incident';
var recordSysId = '8d6353eac0a8016400d8a125ca14fc1f';
var filename = 'snlogo.png';
// Let's download the ServiceNow Logo
var logoUrl = '';
var request = new sn_ws.RESTMessageV2();
// Configure the request to save the response as an attachment
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
// When we execute the request, the attachment will automatically be
// saved to the record we specified
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseContentType = response.getHeader('Content-Type');
gs.debug("http response status_code: " + httpResponseStatus);
gs.debug("http response content-type: " + httpResponseContentType);
// Get the sys_id of the newly created attachment
var newAttachmentSysId = response.getResponseAttachmentSysid();
// Do something useful with it
var grAttach = new GlideRecord('sys_attachment');
if (grAttach.get(newAttachmentSysId)) {
// Note: we're using the scoped version of GlideSysAttachment
// for this example.
var gsa = new GlideSysAttachment()
var content = gsa.getContent(grAttach);
// Now we have the data...the rest is up to you
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment