Forked from jnerius/servicenow_restmessagev2_response_as_attachment.js
Created
October 24, 2019 00:33
-
-
Save nancystodd/6b9d0e44486f08c22a674d75dfed438a 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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