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

Dr Atul G- LNG
Tera Patron
Tera Patron

https://www.servicenow.com/community/itsm-forum/inbound-scripted-rest-api-attachment-issue/m-p/27201...

https://www.servicenow.com/community/developer-forum/attachment-getting-corrupted/m-p/1494214

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

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

@maneesh3 

Hope you are doing good.

Did my reply answer your question?

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

Hi Ankur,

 

This worked perfectly. Now able to receive all formats of files. 

 

Thanks a lot for your support.