
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 01:38 AM
Hi Experts,
Can anyone help me with the code of scripted rest API to add an attachment with request payload?
Thanks & Regards
Prasant Kumar Sahu
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 01:49 AM
Hi,
Sample JSON request:
{
"incidentNumber": "INC000124",
"fileName": "my file.txt",
"contentType": "text/plain",
"fileData": "my sample file"
}
Scripted REST API Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var number = parsedData.incidentNumber;
var fileName = parsedData.fileName;
var fileContentType = parsedData.contentType;
var fileData = parsedData.fileData;
var rec = new GlideRecord('incident');
rec.addQuery('number',number);
rec.query();
if(rec.next()){
var sa = new GlideSysAttachment();
sa.write(rec, fileName, fileContentType, fileData);
var responseBody = {};
responseBody.incNumber = number;
responseBody.status = "Success";
response.setBody(responseBody);
}
else{
var responseBodyFailure = {};
responseBodyFailure.status = "Failure";
response.setBody(responseBodyFailure);
}
})(request, response);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2022 09:06 AM
@Ankur Bawiskar I have a similar requirement. I have few field values to be mapped along with an attachment. I am not able to attach the file from here. How should I pass the file here? I would be passing "image", "pdf", and "excel" files. So the data will have to create a record and attach the file to it. I am using the follwing script:
How should I pass the data in the payload?
{
"incidentNumber": "INC000124",
"fileName": "my file.txt",
"contentType": "text/plain",
"fileData": "my sample file" ------- Should it be base64 encoded text??
}
I tried like this but it did not work
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.dataString;
var parser = new global.JSON();
var parsedData = parser.decode(requestBody);
var number = parsedData.incidentNumber;
var shortDescription = parsedData.shortDescription
var fileName = parsedData.fileName;
var fileContentType = parsedData.contentType;
var fileData = parsedData.fileData;
var rec = new GlideRecord('incident');
rec.initialize("short_description",shortDescription )
var sysID = rec.insert()
var gr = new GlideRecord("incident");
gr.addQuery("sys_id",sysID);
gr.query()
if(gr.next()){
var sa = new GlideSysAttachment();
sa.write(gr, fileName, fileContentType, fileData);
var responseBody = {};
responseBody.incNumber = number;
responseBody.status = "Success";
response.setBody(responseBody);
}
else{
var responseBodyFailure = {};
responseBodyFailure.status = "Failure";
response.setBody(responseBodyFailure);
}
})(request, response);
If you can guide me here, it would be a great help.
Thanks in advance..!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2023 11:57 PM
It is working fine for me when i use below sample JSON in postman application.
it is creating new file attaching . when i directly attach file with binary option.it is not working . kindy let me know how can i send existing file than creating new one?
{
"incidentNumber": "INC000124",
"fileName": "my file.txt",
"contentType": "text/plain",
"fileData": "my sample file"
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 12:41 AM
what do you mean by sending existing file?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 01:30 AM
i mean instead of it creates new file, i would like to attach the image from binary option in postman?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2023 06:25 AM
Ankur this works for plain text file (txt) only. Do you have any generic code that works for jpg, docx etc files ?