Attachment API GET Convert Binary String to File Attachment

Salvador Marcha
Kilo Guru

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 

find_real_file.png

1 ACCEPTED SOLUTION

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi @Salvador Marchan 

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.

View solution in original post

7 REPLIES 7

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi @Salvador Marchan 

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.

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

Getting an error: "Unable to save Responsebody as attachment : User not authorized"

But the user is having admin role.

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