What is the best way to create a Scripted REST API to attach files?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2024 11:39 PM - edited 06-05-2024 11:41 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 02:48 AM - edited 06-07-2024 02:49 AM
Hi @caroll,
I suggest you use the GlideSysAttachment API.
Check out the way to do it from the following link:
https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server/no-namespace/c_GlideSysA...
Reference Links from the community with detailed explanation:
https://www.servicenow.com/community/developer-forum/how-to-create-a-scripted-rest-api-to-attachment...
Please check the document which has the example for your requirement:
https://docs.servicenow.com/bundle/washingtondc-api-reference/page/integrate/custom-web-services/ref...
Some Youtube References:
https://www.youtube.com/watch?v=ptPlW4tQXEw
Please mark helpful and accept as solution if this helped.
Thanks,
Mahathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 03:32 AM
Hi @caroll,
Below is example for the same
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var attachment = new GlideSysAttachment();
var tableName = 'incident'; // Example table name
var recordSysId = request.queryParams.sys_id; // Example: sys_id of the record
var fileName = request.queryParams.fileName;
var fileContent = request.body.data;
// Create attachment record
var attachmentSysId = attachment.write(tableName, recordSysId, fileName, fileContent);
if (attachmentSysId) {
response.setStatus(201);
return {
"attachment_sys_id": attachmentSysId,
"message": "Attachment uploaded successfully"
};
} else {
response.setStatus(500);
return {
"error": "Failed to upload attachment"
};
}
})(request, response);
Please accept my solution if it resolves your issue and thumps 👍 up
Thanks
Jitendra