Inbound Scripted REST API attachment issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 01:40 PM
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.
Scripted REST API:
Kindly help me resolve this.
@Ankur Bawiskar I would really appreciate if you could have look.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 08:35 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-01-2023 09:57 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2023 06:59 AM
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.