- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2021 06:55 PM
SN Community and Experts,
I was able to simulate successfully and get a binary string using REST API Explorer, POSTMAN and created a REST Message script to extract from another ServiceNow instance. However, the returned value is a binary string and I need to attach the file itself to a target record (e.g. Incident record).
Question: How do I exactly convert this binary string to a file and attach to a target record??? (OR, is there a better way of not acquiring binary string but to extract directly the file and create a record in the sys_attachment with the target record's sys_id as table_sys_id using scripts???
I tried GlideSysAttachment (https://docs.servicenow.com/bundle/quebec-application-development/page/app-store/dev_portal/API_reference/GlideSysAttachmentScoped/concept/c_GlideSysAttachmentScopedAPI.html) with the 'getContentBase64' method but I got lost here. Perhaps, I have mixed up the order of scripts and I do need some assistance (IF this is the right approach).
I appreciate in advance.
Thanks,
Dor
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2021 08:01 PM
Hi
You can save attachment through saveResponseBodyAsAttachment. Below is the sample code.
var targetInstanceURL = endpoint;
var targetUserID = username;
var targetUserPassword =password;
var sa = new GlideSysAttachment();
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("get");
//Mid server if you want to run it thorugh mid server
var midServer = gs.getProperty("midServerName", "");
if(midServer){
attachmentMessage.setMIDServer(midServer);
}
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL);
attachmentMessage.saveResponseBodyAsAttachment("incident", incSysId,fileName,fileType);
var response = attachmentMessage.execute();
var httpStatus = response.getStatusCode();
var newAttachmentSysId = response.getResponseAttachmentSysid();
Hope it helps.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2021 08:01 PM
Hi
You can save attachment through saveResponseBodyAsAttachment. Below is the sample code.
var targetInstanceURL = endpoint;
var targetUserID = username;
var targetUserPassword =password;
var sa = new GlideSysAttachment();
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("get");
//Mid server if you want to run it thorugh mid server
var midServer = gs.getProperty("midServerName", "");
if(midServer){
attachmentMessage.setMIDServer(midServer);
}
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL);
attachmentMessage.saveResponseBodyAsAttachment("incident", incSysId,fileName,fileType);
var response = attachmentMessage.execute();
var httpStatus = response.getStatusCode();
var newAttachmentSysId = response.getResponseAttachmentSysid();
Hope it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-12-2021 09:01 PM
YOU ARE THE MAN!!!!!! It worked perfectly.
I currently have a REST Message, Script Includes, which has a function (shown below) that invokes a REST Message (therefore, other variables and definition are already set)...BUT the key here is 'saveResponseBodyAsAttachment' and I read through the SN Docs as well (https://docs.servicenow.com/bundle/rome-application-development/page/app-store/dev_portal/API_reference/RESTMessageV2/concept/c_RESTMessageV2API.html)
getFileContentAsAttachment: function (argSourceFileSysID, argTargetTable, argTargetSysID, argFileName){
try{
var r = new sn_ws.RESTMessageV2(this.RESTAPIAttachment, this.GETFileContents);
r.setStringParameterNoEscape('file_sys_id', argSourceFileSysID);
r.setAuthenticationProfile(this.AUTH_TYPE, this.PROFILE_NAME);
r.saveResponseBodyAsAttachment(argTargetTable, argTargetSysID, argFileName);
var response = r.execute();
//var httpStatus = response.getStatusCode();
//gs.info("Dor Test - http response status_code: " + httpStatus);
} catch (ex) {
var message = ex.getMessage();
gs.info(message);
}
}
I'll be doing more test but I believe it's already there.
Thank you! Dor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2022 05:55 AM
Getting an error: "Unable to save Responsebody as attachment : User not authorized"
But the user is having admin role.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2023 11:56 PM - edited ‎09-29-2023 12:08 AM
Hi There,
If I have to do this from Third-party. (means send file from third party without converting it into binary/base64) Is there any possible solution?
Thank you