Scripted Rest Api - Base 64 or binary conversion

Syed zabiullah
Tera Expert

I have attachments up to 30 MB in size that I need to send to a third-party system. I've created a Scripted REST API that uses base64 encoding to convert the files, but it only works for files up to 2 MB. For files exceeding 5 MB, the conversion fails. Is there any approach to migrate these files to the third-party system?


We tested the out-of-the-box Attachment API and received a response like the following. How can a third party convert that response into a file and attach it to their system (for example, Workday)?

Syedzabiullah_0-1763970751954.png

Give me solution for this requirement!

 

6 REPLIES 6

@Syed zabiullah  

then this will work for more than 5MB

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

	var StringUtil = new GlideStringUtil();
	var gsis = GlideSysAttachmentInputStream(gr.sys_id.toString());
	var ba = new Packages.java.io.ByteArrayOutputStream();
	gsis.writeTo(ba,0,0);
	ba.close();
	var base64EncodedData = StringUtil.base64Encode(ba.toByteArray());
gs.info(base64EncodedData);

-> the maximum attachment size that the system will return for base64 encoding for is controlled by this system property "com.glide.attachment.max_get_size".

-> If the property does not exist it will default to maximum 5 MB.

-> If that property is not there then create it and set the value as per your need.

However, as mentioned in the following KB, this may cause some issues.

Large attachments may cause Out Of Memory errors and performance degradation 

Server side script fails with error "String object would exceed maximum permitted size of 33554432" ... 

OR

Another way is to use GlideScriptableInputStream and GlideTextReader

This is the video for the same

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Syed zabiullah  

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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