- 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
08-10-2024 10:25 AM
Hi Ankur,
Using your above script for attachements creation along with creation of incident , i have written the below code. But I am getting 500 internal error. with the below error message.
{
"error": {
"message": "Script Evaluation Exception",
"detail": "Can't find method com.glide.ui.SysAttachment.write(string,undefined,undefined,undefined). (sys_ws_operation.f5d38f17ffbfc210b9bff9337c4fd93d.operation_script; line 37)"
},
"status": "failure"
}Can you help me sorting this issue:
"short_description": "Issue with mobile device",
"description": "Issue with mobile device",
"level1": "iphone",
"level2": "Charging",
"attachments": [
{
"fileName": "example.txt",
"contentType": "text/plain",
"content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQu"
}
]
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 12:50 AM
Hi Ankur,
I have similar requirement where I need to create an incident and attach the file while creating and also the attachment is muti form such as image and othe types of files, I tried various scripts but multi form is not working. can you help me in this.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2024 06:34 AM
Hi Naveen,
If you are writing a scripted Rest API , it will be useful. I could attach all media, vedios/images/text files. Please give thumbs up if it works for you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 02:02 AM
Hi Prashant.
Check out this link. It has good information with examples.
Secondly, you can use REST API Explorer and build an request there. Then once done, in that page you can click on ServiceNow Script which gives you complete script to update the incident.
Mark the comment as a correct answer and also helpful once worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 08:16 AM
Hello Prashant,
If this has answered your question, kindly mark the comment as a correct answer so that the question is moved to the solved list and others looking for similar solution can be benefitted from it.
