Help with writeBase64 in GlideSysAttachment

Abhijit Das7
Tera Expert

Hi Everyone,

 

I am trying to send base64 image as attachment to sys_cs_collab_chat table using GlideSysAttachment , but it not attaching and not even returning any error.

 

Scripted Rest API:

 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here

    var requestBody = request.body.dataString;
    var parser = new global.JSON();
    var parsedData = parser.decode(requestBody);
    var imageName = parsedData.imageName;
    var imageType = "image/jpeg"; 
    var imageValue = parsedData.imageValue;
    var table = parsedData.object_name;
    var sysId = parsedData.object_id;
   
    var rec = new GlideRecord(table);
    rec.addQuery('sys_id', sysId);
    rec.query();
    if (rec.next()) {
        if (imageName && imageValue && imageType != '') {
            var sa = new GlideSysAttachment();
            sa.writeBase64(rec, imageName, imageType, imageValue);
        }         
    }
})(request, response);

 

 

 

JSON payload;

{"object_id":"ea09239a97d00290c0ebd404a253af08",
"imageName":"QjjeAKNbCwNoJwEKrg1dObUx.jpg",
"imageValue":"<<base64FileContent>>",
"object_name":"sys_cs_collab_chat"}

 

Can anyone point out my mistake. 

 

cc: @Ankur Bawiskar 

 

Thanks in advance.

 

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

are you in scoped app?

Did you check the script is going inside the if statement where the GlideSysAttachment code is present?

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

Hi @Ankur Bawiskar ,

No, it is in global scope. In code I am using: 

var sa = new global.GlideSysAttachment();

and it is going inside the if loop of GlidesysAttachment, I placed gs.info in loop and checked it.

 

Thanks

 

@Abhijit Das7 

writeBase64 won't work in global scope. that method is only for scoped app.

you need to use SOAP Attachment creator for this

if (imageName && imageValue && imageType != '') {
    var ecc = new GlideRecord('ecc_queue');
    ecc.initialize();
    ecc.agent = "AttachmentCreator";
    ecc.topic = "AttachmentCreator";
    ecc.name = imageName + ":" + imageType;
    ecc.source = table + ":" + rec.sys_id;
    ecc.payload = imageValue;
    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

Hi @Ankur Bawiskar 

 

I changed it into Scoped application. Now I can see it in attachment table. But I am not able to see it in sidebar chat.

 

Thanks