How to send attachment as a binary data as one of the parameter inside the body to the third party??

Manikantahere
Tera Contributor

When I tried to use the below code to convert the attachment to binary data I got the error. When explored more on the error I found the article saying this will only work scope other than global. is it right? if not how can I convert the attachment into binary data to send it to third-party  ???

 

att-1.png

att-2.png

9 REPLIES 9

@Manikantahere 

getBytes() function require GlideRecord object of sys_attachment but you are sending sysId

So assuming "attachment_list" is the correct name of input variable and it holds sysAttachment sysId, the below script should work fine

var attGr = new GlideRecord('sys_attachment');
attGr.addQuery('sys_id', inputs.attachment_list);
attGr.query();
if (attGr.next()) {
    var gsu = (typeof GlideStringUtil != 'undefined') ? (GlideStringUtil) : (Packages.com.glide.util.StringUtil); //few versions support the first one, other supports second
    var gsa = (typeof GlideSysAttachment != 'undefined') ? (new GlideSysAttachment()) : (new Packages.com.glide.ui.SysAttachment());
    var attachmentData = gsa.getBytes(attGr);
    var attachment = String(Packages.java.lang.String(attachmentData));
    var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
    gs.info("attachment bae64 encoded data is" + encData);
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I am neither getting log nor error when I used above.  can you check once.

 

@Manikantahere 

I am able to get it.

Are you sure you gave the correct sys_attachment sysId as input to your action and using correct input variable name?

As per your earlier screenshot the variable name is "Attachment List" but in script you are using wrong spelling -> inputs.attachemntList

Hope you are testing it correctly

Output for script I shared

base64 global scope.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Manikantahere 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Manikantahere 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader