
- 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
01-13-2021 02:19 AM
Thanks for marking my response as helpful.
If my response helped you please mark it correct to benefit future readers as well.
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
01-13-2021 08:11 AM
Thanks for marking my response as helpful.
If my response helped you please mark it correct to benefit future readers as well.
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
01-17-2021 11:18 PM
Thanks for marking my response as helpful.
If my response helped you please mark it correct to benefit future readers as well.
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
01-19-2021 08:47 AM
Thanks for marking my response as helpful.
If my response helped you please mark it correct to benefit future readers as well.
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-04-2021 06:57 AM
Thanks for marking my response as helpful.
If my response helped you please mark it correct to benefit future readers as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader