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

@Mohammed Masi U 

don't decode it, the function write() requires base64encoded data and not decoded data

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 = attachmentBase64;
var isFileAttached = testUtil.addAttachment(correlationId, fileName, fileType, fileContent);
}

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

Amit Gujarathi
Giga Sage
Giga Sage

HI @Mohammed Masi U ,
II trust you are doing great.
Please find the updated code for script include and scripted rest API

Script include function

addAttachment: function(contractNo, fileName, fileType, attachmentBase64) {
    var queryString = 'number=' + contractNo;
    var contractObj = this.getGRObjectByEncodedQuery('ast_contract', queryString);
    if (contractObj != null) {
        var attachmentRec = new GlideSysAttachment();
        var attachmentSysID = attachmentRec.writeBase64(contractObj, fileName, fileType, attachmentBase64);
        if (JSUtil.notNil(attachmentSysID)) return attachmentSysID;
        else return null;
    }
}

 

Scripted Rest API code

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 isFileAttached = testUtil.addAttachment(correlationId, fileName, fileType, attachmentBase64);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi, Thanks for the Code. However, this is not working and now it is not attaching the file with the above code. Kindly assist.