
- 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
02-15-2023 07:20 AM
for other file you can use SOAP Attachment creator and you require the following details
file name, content type, base64encoded data, record sysId, table name
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-21-2023 05:41 AM
Ankur can you share a code for soap attachment creator. Please share a sample code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2023 06:16 AM
can you post a new question and tag me there as this is quite older thread?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2023 04:46 AM
HI
Same script I used for incident table and through post man I tried but for Doc and Excl.
I used the
{
"incident Number": "INC4969810",
"filename": "test_6.doc",
"content Type": "application/octet-stream",
"file Data": ""
}
and for Excl
{
"incident Number": "INC4969810",
"filename": "test_8 Equipment Delivery.xlsx",
"content Type": "application/xlsx",
"file Data":""
}
And the file is attaching to the attachment table but when I try to open from the INC4969810, I am getting this one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2024 11:47 PM
Hi Ankur,
I used this approach ,to add attachment to RITM record.I am doing exactly the same.
this is my json on postman: