Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Inbound Scripted REST API attachment issue.

Mohammed Masi U
Tera Contributor

Hi All,

I've created a Scripted REST API to decode the base64encode attachment and have it attached to the target record. It's working fine for .txt and .csv . However, it doesn't work with .docx/png/xlsx and gives an error while opening the file "we're sorry. we cannot open this file because we have found problem with its content, either its corrupted..etc"

* I've tried  writeBase64 it doesn't work at all and the Example code provided in the document, I've tried it in Background Script. https://developer.servicenow.com/dev.do#!/reference/api/utah/server/no-namespace/c_GlideSysAttachmen... 
This doesn't work. 

MohammedMasiU_0-1698869191034.png

Scripted REST API: 

var requestBody = request.body.dataString;
    // Parse the extracted JSON data to convert it into a JavaScript object.
    requestBody = JSON.parse(requestBody);
var fileName = requestBody.file.fileName;
    var fileType = requestBody.file.contentType;
    var attachmentBase64 = requestBody.file.fileData;
var testUtil = new global.AB_testUtils();
if (JSUtil.notNil(attachmentBase64)) {
                var fileContent = GlideStringUtil.base64Decode(attachmentBase64);
                var isFileAttached = testUtil.addAttachment(correlationId, fileName, fileType, fileContent);
                
}
 
Script Include(attachment part):
 
addAttachment: function(contractNo, fileName, fileType, fileContent) {
        var queryString = 'number=' + contractNo;
        var cotractObj = this.getGRObjectByEncodedQuery('ast_contract', queryString);
        if (cotractObj != null) {
            var attachmentRec = new GlideSysAttachment();
            var attachmentSysID = attachmentRec.write(cotractObj, fileName, fileType, fileContent);
            if(JSUtil.notNil(attachmentSysID)) return attachmentSysID;
else return null;
        }



Kindly help me resolve this.
@Ankur Bawiskar I would really appreciate if you could have look.

Thanks!

 

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@Mohammed Masi U 

what about pdf or other file?

 

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

Hi @Ankur Bawiskar Thanks for your quick response. PDF and Other files are also not working.

@Mohammed Masi U 

are you in global scope or custom scope?

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

Ankur, I'm working in Global scope.