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

Hi Ujjawal,

Thankyou so much for posting the solution , it helped me a lot

Petr Pastuszek1
Tera Contributor

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

PetrPastuszek1_0-1708009131426.png

 

MohamedAliE
Tera Contributor

in my BR : i wrote this : 

function sendAttachment(targetSysId) {
    var grAttach = new GlideRecord('sys_attachment');
    grAttach.addQuery('table_sys_id', current.sys_id);
    grAttach.query();

    while (grAttach.next()) {
        var fileName = grAttach.file_name.toString();
        var contentType = grAttach.content_type.toString() || 'application/octet-stream';

        var binData = new GlideSysAttachment().getBytes(grAttach);

        try {
            var rm = new sn_ws.RESTMessageV2('Upload Attachment to LVER DEV', 'POST');

            // Endpoint avec sys_id cible et nom de fichier
            var endpoint = rm.getEndpoint()
                .replace('${table_name}', 'incident')
                .replace('${table_sys_id}', targetSysId)
                .replace('${file_name}', encodeURIComponent(fileName));
           
            rm.setEndpoint(endpoint);
            rm.setHttpTimeout(60000);

            // Le binaire directement dans le body
            rm.setRequestHeader("Content-Type", contentType);
            rm.setRequestHeader("Accept", "application/json");
            rm.setRequestBody(binData);

            var resp = rm.execute();
            var body = resp.getBody();
            var status = resp.getStatusCode();

            gs.log("Attachment sent: " + fileName + " | MIME type: " + contentType + " | Status: " + status + " | Response: " + body);
        } catch (e) {
            gs.log("Error sending attachment " + fileName + ": " + e.message);
        }
    }
}
 
but in my instance cible the file are truncated 😞