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 any suggestions as to how to make a whole signature fit into the attachment generated when using 

writeBase64(rec, imageName, imageType, imageValue);

my attachment is generated but the signature gets cut off when it's longer then a certain length

 

Hi @Ankur Bawiskar ,

 

after your above suggestion,

I used the ECC_Queue to insert an attachment record and create it in the Attachment Table, which should link it to the Incident record. While this process worked, it resulted in the creation of two separate records in the Sys_attachment form. Consequently, the Incident form now has two attachments (if I map the file name and Table sys ID)else it will be single on the incident form. 

 

1. The first attachment has empty details because I haven’t mapped any fields from the attachment table, as it will be appended in the Incident form with a 0 bytes file. I’m sending all the necessary fields through the ECC Queue in the script. (This record sysid I can see in response while testing in Postman)

   

2. The second attachment record contains proper details with all fields mapping and decoded files  This record Sysid is not coming in Response.

 

Can you please provide insight into my post?

 

https://www.servicenow.com/community/developer-forum/2-records-inserted-in-attachment-table/m-p/3081...

 

I appreciate any help you can provide.

Ashwini