Attachments to base64

harshi_ramesh
Tera Expert

Hi people

 

Need a solution regarding how we can convert attachments to base64 and sent it via API.

So, basically I have created a catalog and have been sending the info added onto the fields from the catalog via API to 3rd party(all this done using flow).

 

  • Now, I need to send the attachments as well through the same API trigger. Currently I have been using "LookUp Record" step, attaching screenshot. 
  • But the issue is, say we have 2 attachments, the payload goes twice. As we have "For Each", but I need it to be sent in one trigger. Something like this:
<attachments>
<z_attachment>
<base64>{base64_value}</base64>
</z_attachment>
<z_attachment>
<base64>{base64_value}</base64>
</z_attachment>
</attachments>
  • I was able to sent 'hash value', but need to send as base64. How?

 

I would appreciate your help!! Let me know if further details are required.

 

Thanks

1 ACCEPTED SOLUTION

@harshi_ramesh 

then here is the alternative for scoped app

var gr = new GlideRecord('sys_attachment');
gr.get('3fd6017407d3dc50540bf2508c1ed027');

var sysAtt = new GlideSysAttachment();

var base64Data = sysAtt.getContentBase64(gr);

gs.info(base64Data);

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

View solution in original post

5 REPLIES 5

Martin Virag
Tera Guru

you can use the getContentBase64 method in the glidesysattachment class for this purpose.
https://developer.servicenow.com/dev.do#!/reference/api/yokohama/server/no-namespace/c_GlideSysAttac...

For your other question:after the foreach create 1 object payload and then trigger the interface with that

Shubham_Jain
Mega Sage

@harshi_ramesh  See if this helps you. 

 

var targetSysId = '917775e6836622100c8bec80ced3fff1d'; 
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_sys_id', targetSysId);
attachmentGR.query();
var attachmentsXML = '<attachments>';
while (attachmentGR.next()) {
    var attachmentData = new GlideSysAttachment().getBytes(attachmentGR);
    var base64String = GlideStringUtil.base64Encode(attachmentData);
    attachmentsXML += '<z_attachment><base64>' + base64String + '</base64></z_attachment>';
}
attachmentsXML += '</attachments>';
gs.info(attachmentsXML); 

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


Ankur Bawiskar
Tera Patron
Tera Patron

@harshi_ramesh 

You won't get base64 directly and it's not stored directly on any field.

since you want to combine I will suggest this

1) create a flow variable of type string

2) then use GlideRecord on sys_attachment and iterate and form the complete XML string

3) then use this flow variable and send it in API

Sample script like this to get base64 encoded data

I hope with this you can enhance it further.

Note: This is only for global scope

var currentSysId = fd_data.trigger.current.sys_id;
var attGr = new GlideRecord('sys_attachment');
  attGr.addQuery('table_sys_id', currentSysId);
  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));
   gs.info(attachment); //the data in the file will be printed as String
   var encData = GlideStringUtil.base64Encode(attachmentData); // data of one attachment at a time
   gs.info(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

Hi @Ankur Bawiskar 

 

Thanks for the reply.

But my flow is in a custom scope. Any alternative option.

 

Thanks