- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 04:53 AM
Hello,
I am trying to send an attachment via Scripzted Rest API from Postman to ServiceNow for the incident table.
For the attachment I have written the below code in Global scope application.
I am receiving an error "
"
Kindly let me know what is the exact issue in the code
Regards,
Dipanjan Saha
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try {
var requestBody = request.body.data;
var shortDescription = requestBody.short_description;
var description = requestBody.description;
var fileData = requestBody.file_data; // Base64 encoded file data
var fileName = requestBody.file_name;
var contentType = requestBody.content_type; // e.g., "image/png"
// Log the incoming request data for debugging
gs.info('Request received: shortDescription=' + shortDescription + ', description=' + description + ', fileName=' + fileName + ', contentType=' + contentType);
// Create Incident
var incidentGr = new GlideRecord('incident');
incidentGr.initialize();
incidentGr.short_description = shortDescription;
incidentGr.description = description;
var incidentSysId = incidentGr.insert();
gs.info('Incident created with sys_id=' + incidentSysId);
// Validate the file data
if (!fileData || !fileName || !contentType) {
throw new Error('Missing file data, file name, or content type');
}
// Attach file to incident
var attachment = new GlideSysAttachment();
var decodedFileData = GlideStringUtil.base64DecodeAsBytes(fileData);
attachment.write('incident', 'incidentSysId', 'fileName', 'contentType', 'decodedFileData');
gs.info('File attached to incident with sys_id=' + incidentSysId);
// Return the incident sys_id
var responseBody = {
sys_id: incidentSysId
};
response.setStatus(201);
response.setBody(responseBody);
} catch (ex) {
gs.error('Error processing request: ' + ex.message);
var errorResponse = {
error: ex.message
};
response.setStatus(400);
response.setBody(errorResponse);
}
})(request, response);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 05:54 AM - edited 07-22-2024 05:59 AM
The error suggests you may not be using the GlideSysAttachment class correctly. Looking at the documentation, it seems to support that.
See below:
GlideSysAttachment - Global (servicenow.com)
Looking at your script above, you have fed .write() 5 strings instead of feeding it parameters inline with the image above
I would have expected:
var attachment = new GlideSysAttachment();
var decodedFileData = GlideStringUtil.base64DecodeAsBytes(fileData);
attachment.write(incidentGr, requestBody.file_name, requestBody.content_type, decodedFileData);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 02:16 AM - edited 07-23-2024 02:17 AM
GlideStringUtil.base64DecodedAsBytes does not seem to be documented, so I was skeptical around how it worked. I have tested it with an example in background script and it appears it works fine (provided you feed it a base64encoded string). Documentation here:
GlideStringUtil - Scoped, Global | ServiceNow Developers
In your postman example above you don't provide the base64 encoded image, can you confirm it's actually sending this and not "<convert your image to base 64 and paste it here>"
This is the background script example that does work, proving it should work if fed a base64 encoded image
var gr = new GlideRecord('incident');
gr.get('03b2b73d83eb8210e51a1530ceaad394');
var type = "image/png";
var content = "a base 64 encoded image, which this site does not allow me to include as it breaches some kind of policy";
var contentDecoded = GlideStringUtil.base64DecodeAsBytes(content);
new GlideSysAttachment().write(gr, 'testpng.png', type, contentDecoded);
I created the base64 string using the snipping tool and encoded using a website but I was prevented from including it in the above example due to some form of restriction
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 04:53 AM
Hello,
It worked for me now.. The url i was using to convert the image to base 64 was giving a wrong output so I changed the URL to "https://base64.guru/converter/encode/image" and converted my image.
now everything is working fine for me.
Thanks a lot for your help.