unable to get pdf, jpg, png etc in REST inbound attachment

maneesh3
Tera Contributor

Hi Team,

 

Unable to receive correct files , getting corrupted attachments in REST inbound. receiving only text files. 

Should be able to get PDF, JPG, PNG , excel files as welll..

below is my attachment handling code:

 

 

    if (JSUtil.notNil(file_name) && JSUtil.notNil(content_type) && JSUtil.notNil(base64Data) && JSUtil.notNil(correlationID)) {


       
        var caseForAttachment = new GlideRecord('sn_customerservice_case');
        if (caseForAttachment.get('correlation_id', correlationID)) {

            try {
                var sa = new GlideSysAttachment();
            //  var decodedData = GlideSysAttachment.base64Encode(base64Data);
                var decodedData = GlideStringUtil.base64Encode(base64Data); // Decode base64 content
                 
                sa.write(caseForAttachment, file_name, content_type, decodedData);
 
 
 
Please suggest here. thanks a lot for help
 
 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@maneesh3 

are you writing this in scripted REST API?

Where are you writing this?

GlideSysAttachment write() method only works for plain text file such as .txt or .csv

AnkurBawiskar_0-1738066317987.png

 

If you are getting base64 encoded data then use this and it will work for all types

    if (JSUtil.notNil(file_name) && JSUtil.notNil(content_type) && JSUtil.notNil(base64Data) && JSUtil.notNil(correlationID)) {
        var caseForAttachment = new GlideRecord('sn_customerservice_case');
        if (caseForAttachment.get('correlation_id', correlationID)) {
            var eccGr = new GlideRecord('ecc_queue');
            eccGr.initialize();
            eccGr.setValue('agent', 'AttachmentCreator');
            eccGr.setValue('topic', 'AttachmentCreator');
            eccGr.setValue('name', file_name + ':' + content_type);
            eccGr.setValue('source', 'sn_customerservice_case' + ':' + caseForAttachment.sys_id);
            eccGr.setValue('payload', base64Data);
            eccGr.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

View solution in original post

5 REPLIES 5

Glad to help.

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