Convert binary to file attachment

Thiago Macedo
Tera Contributor

Hi there,
The question i have is, i'm using the rest messages to access an endpoint that retuns me a binary that corresponds to a zip file.. is there any method that i can use to create from this binary a zip attachment on a specified record?

1 REPLY 1

Daniel Borkowi1
Mega Sage

Hi @Thiago Macedo, within a script, maybe placed in a Scripted Rest API, you can use GlideSysAttachment.

 

var attachment = new GlideSysAttachment();

var rec = new GlideRecord('incident');
var incidentSysID = 'ab1b30031b04ec101363ff37dc4bcbfc';
rec.get(incidentSysID);
var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';

var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);

gs.info('The attachment sys_id is: ' + agr);

 

If you want to insert the Attachment from an other system you can use the Attachment API.

 

curl "https://instance.servicenow.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot" \
--request POST \
--header "Accept:application/json" \
--header "Content-Type: image/jpeg" \
--user "username":"password" \
--data-binary "@ location of the file on file system"

Greets
Daniel

Please mark reply as Helpful/Correct, if applicable. Thanks!