Attaching attachment via scripted rest api
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2024 12:56 AM
I'm trying to create a scripted rest api what would handle attachments coming in together with the JSON integration message. So when we receive the integration message on that specific endpoint, it does create the request and request item as supposed to as long as there is no attachment. But as soon as I there are attachment added it will geenrate this error: "
API:
(function process(request, response) {
try {
// Parse the request body to get the JSON data
var requestBody = request.body.data;
var message = requestBody.message;
var fileName = message.file_name;
// Validate that the file is attached to the request
var attachment = new GlideSysAttachment();
var attachmentSysId = null;
var attachmentGr = new GlideRecord('sys_attachment');
attachmentGr.addQuery('table_sys_id', request.getParameter('sys_id'));
attachmentGr.addQuery('file_name', fileName);
attachmentGr.query();
if (attachmentGr.next()) {
attachmentSysId = attachmentGr.sys_id.toString();
} else {
throw new Error("Attachment with file name " + fileName + " not found.");
}
// Create a new request [sc_request]
var scRequest = new GlideRecord('sc_request');
scRequest.initialize();
scRequest.requested_for = gs.getUserID();
scRequest.requested_by = gs.getUserID();
scRequest.short_description = 'Request created via Scripted REST API';
var requestSysId = scRequest.insert();
// Create a new request item [sc_req_item]
var scReqItem = new GlideRecord('sc_req_item');
scReqItem.initialize();
scReqItem.request = requestSysId;
scReqItem.cat_item = '3f59c277476282103553cd84f26d43b1';
scReqItem.quantity = 1;
scReqItem.short_description = 'Request Item created via Scripted REST API';
var reqItemSysId = scReqItem.insert();
// Attach the file to the request [sc_request]
if (attachmentSysId) {
attachment.copy('sys_attachment', attachmentSysId, 'sc_request', requestSysId);
}
// Response JSON
var responseBody = {
status: "success",
requestSysId: requestSysId,
reqItemSysId: reqItemSysId
};
response.setStatus(201);
response.setBody(responseBody);
} catch (ex) {
var errorResponse = {
status: "error",
message: ex.getMessage()
};
response.setStatus(500);
response.setBody(errorResponse);
}
})(request, response);
another try I did with an API:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.data;
// Validate the JSON input
if (!requestBody.message || requestBody.message.name !== 'CMDB Update' || !requestBody.message.file_name) {
response.setStatus(400);
return response.setBody({error: 'Invalid request: name must be "CMDB Update" and file_name cannot be empty'});
}
var fileName = requestBody.message.file_name;
var attachmentSysId = findAttachment(request, fileName);
if (!attachmentSysId) {
response.setStatus(400);
return response.setBody({error: 'Attachment not found or does not match the file_name'});
}
// Create the request
var scRequest = new GlideRecord('sc_request');
scRequest.initialize();
scRequest.setValue('short_description', 'CMDB Update Request');
scRequest.insert();
// Create the request item
var scReqItem = new GlideRecord('sc_req_item');
scReqItem.initialize();
scReqItem.setValue('request', scRequest.sys_id);
scReqItem.setValue('cat_item', '3f59c277476282103553cd84f26d43b1');
scReqItem.setValue('short_description', 'CMDB Update Request Item');
scReqItem.insert();
// Create the task
var scTask = new GlideRecord('sc_task');
scTask.initialize();
scTask.setValue('request', scRequest.sys_id);
scTask.setValue('request_item', scReqItem.sys_id);
scTask.setValue('short_description', 'CMDB Update Task');
scTask.insert();
// Copy the attachment to the task
copyAttachment(attachmentSysId, scTask.sys_id);
// Respond with success
response.setStatus(200);
response.setBody({message: 'Request, Request Item, and Task created successfully'});
// Function to find attachment by name
function findAttachment(request, fileName) {
var attachmentSysId;
var attachments = new GlideRecord('sys_attachment');
attachments.addQuery('table_name', request.getTableName());
attachments.query();
while (attachments.next()) {
if (attachments.file_name.toString() === fileName) {
attachmentSysId = attachments.sys_id.toString();
break;
}
}
return attachmentSysId;
}
// Function to copy attachment
function copyAttachment(sourceSysId, targetSysId) {
var sourceAttachment = new GlideRecord('sys_attachment');
if (sourceAttachment.get(sourceSysId)) {
var attachment = new GlideSysAttachment();
attachment.copy('sys_attachment', sourceSysId, 'sc_task', targetSysId);
}
}
})(request, response);
but here the error says "
could somebody help me why I can't create a request and add the attachment to this created request?
The attachment is a .json file and is attached to the integration message based on what the request will be created.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2024 12:59 AM - edited 06-18-2024 01:01 AM
Hi,
Do you say that when your payload contains attachment it is not even creating a RITM?
If you are able to create and adding attachment is the issue, pass RITM ID as the input parameter on the method copyAttachment
Task might have restrction on adding attachments