- 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
‎08-07-2024 08:24 AM
Hi Ujjawal,
Thankyou so much for posting the solution , it helped me a lot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2024 06:58 AM
Im getting same problem, exactly same result and dont know how to create attachment in SCRIPTED REST API, any advice please?
This is my response.
need help please.
/Petr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
in my BR : i wrote this :