- 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-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-22-2024 02:39 PM
Thank you and now the incident is getting created with the attachment getting added as well. But after downloading the file from the ServiceNow incident form, i am getting the attached error message.
The body that i am sending from the postman is
{
"short_description": "Issue with Application_Dipanjan",
"description": "Detailed description of the issue_Dipanjan",
"file_name": "issue_dipanjan.png",
"content_type": "image/png",
"file_data": "<convert your image to base 64 and paste it here>"
}
can you let me know the issue in the body of the POSTMAN?
Thanks in advance.
Regards,
Dipanjan Saha
- 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 02:57 AM
Hello,
I tried using the background script that you provided and the image alo got attached to the target incident record. However when i downloaded the image the same issue is showing up. The Base 64 image code cannot be copied here since the content does not allow more than 250,000 characters.
My email id id dipanjansaha1991@gmail.com.. Can you send me the base 64 code there? or I can send mine to your email address just to verify if this is a system issue.
Thanks in advance.
Regards,
Dipanjan Saha