Scripted Rest API to parse file and attach to respective user's case record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 03:48 AM
Hi ,
Can anyone help me with the below code created for scripted rest API to parse file attachment coming in request payload.
Requirement is we get the attachment and user id from source system , then we need to attach that file to the respective case created for that user. For testing this requirement , I started developing this and created scripted rest API and trying to send the file from postman. I am getting the below mentioned error when the API is invoked in postman.
Scripted Rest API created in Scoped Application.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var fileData = parsedData.fileData;
var rec = new GlideRecord('sn_hr_core_case');
rec.addQuery('number','HRC0003126');
rec.query();
if(rec.next()){
if (userid == '12345'){
var sa = new Glidesysattachment();
sa.write(rec, fileName, fileContentType, fileData);
response.setStatus(200)
var responseBody = {};
responseBody.message = "user file received successfully";
response.setBody(responseBody);
}
}
else{
var responseBodyFailure = {};
responseBodyFailure.status = "Failure";
response.setBody(responseBodyFailure);
}
})(request, response);
Below is the screenshot in Postman to send the file and Error message I am getting.
Please let me know how to successfully perform this POC and test this requiremet to test the file upload from source system to ServiceNow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 03:55 AM
Hi @Mrman
Please try this-
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var fileData = parsedData.fileData;
var fileName = parsedData.fileName; // Assuming fileName is sent in the request
var fileContentType = parsedData.fileContentType; // Assuming fileContentType is sent in the request
var userid = parsedData.userid; // Assuming userid is sent in the request
if (!fileData || !fileName || !fileContentType || !userid) {
response.setStatus(400); // Bad Request
response.setBody({error: "Missing required parameters"});
return;
}
var rec = new GlideRecord('sn_hr_core_case');
rec.addQuery('number', 'HRC0003126');
rec.query();
if (rec.next()) {
// Assuming userid is a field in the case record
if (rec.getValue('userid') == userid) {
var sa = new GlideSysAttachment();
sa.write(rec, fileName, fileContentType, fileData);
response.setStatus(200);
var responseBody = {};
responseBody.message = "User file received successfully";
response.setBody(responseBody);
} else {
response.setStatus(403); // Forbidden
response.setBody({error: "User ID does not match"});
}
} else {
response.setStatus(404); // Not Found
response.setBody({error: "Case not found"});
}
})(request, response);
Regards.
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 04:04 AM
Hi @Amit Pandey ,
I tried the script you provided, it is still giving below error message in response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 04:27 AM
Then you will have to use Attachment API.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2024 04:34 AM
Hi Amit,
I need to do additional scripting like send a response message and move this file attachment to the document management table . I think can be done only with scripted rest api. Please guide me how to achieve this .