Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustamtm/4ae51392ebcf8b7f4be7760e5e73efd0 to your computer and use it in GitHub Desktop.
Save rustamtm/4ae51392ebcf8b7f4be7760e5e73efd0 to your computer and use it in GitHub Desktop.
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 = 'https://instance.service-now.com/images/logos/logo_service-now.png';
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint(logoUrl);
// 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