how to create attachment in sys_attachment table from payload

tanz
Tera Expert

Hi,

I have a payload from which I want to initialize and create an attachment in the sys_attachment table. The table sys_id and table name is a73d38761b55b9906b89a642b24bcbff and sc_cart_item. Could you please help on how to initialize and create an attachment from the json payload in sys_attachment table.

{

                "payload": "", //base64 format already given

                "file_name": "d.xlxs",

                "content_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

                "encoding": "base64"

            }

 

Can you please help on the scripting part?

6 REPLIES 6

Daniel Borkowi1
Mega Sage

Hi @tanz , you can use GlideSysAttachment for this.

 

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);

 

 

Greets
Daniel

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

This is not working.

The response is undefined.

 

I do not have a incident record. It is from sc_cart_item.

@tanz this script is only a example, for sure instead of incident sys_id you can use other records to at attachments, so also sc_cart_item.

 

 

var attachment = new GlideSysAttachment();

var rec = new GlideRecord('sc_cart_item');
var sc_cart_itemSysID = 'a73d38761b55b9906b89a642b24bcbff ';
rec.get(sc_cart_itemSysID);
var fileName = 'example.xlsx';
var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
var base64Encodedcontent = 'SSBhbSB0ZXh0Lg==';

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

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

 

 

Which response is undefined?

To be honest the community members invest a lot of effort to support here, but we can't know what you have in mind. You need to describe your use case and issues in more detail so that we can help. Maybe it's also worth to put in some of your script and screenshot to give you more specific hints about what you did wrong.

 

Greets
Daniel

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

I mean to say I do not have any record to create an attachment, no record sys_id. I just want to initialize and insert an attachment in the attachment table from the json file