Decode base64 image and add as attachment to the incident

Madhavi2
Tera Contributor

Hi All,

I am trying add a base 64 encoded attachment to the incident with below script. But it is not attaching the image.

 

var base64String = current.u_image_data;
var base64Bytes = GlideStringUtil.base64DecodeAsBytes(base64String);
var attachment = new GlideSysAttachment();
var attachmentSysId = attachment.writeBase64(current.getTableName(),current.getUniqueValue(),     'test.jpeg','image/jpeg',base64String);
 
Thanks
5 REPLIES 5

Kieran Anson
Kilo Patron

You're using undocumented APIs.

 

Where is the content of current.u_image_data coming from?

Hi,

Data is coming through import sets mapping it to custom filed in servicenow to to decode and attach the image.

Thanks

@Madhavi2 

check my below comment and working logic

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Madhavi2 

writeBase64 works for scoped app

you are writing this code in global scope? are you writing this in onAfter transform script?

if yes then you can use SOAPAttachment Creator for this, something like this

var ecc = new GlideRecord('ecc_queue');
ecc.initialize();
ecc.agent = "AttachmentCreator";
ecc.topic = "AttachmentCreator";
ecc.name = "test.txt:text/plain";
ecc.source = "kb_knowledge:" + target.sys_id;
ecc.payload = source.u_base64Data;
ecc.insert();

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