- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2024 08:01 AM
I am working on thing in Scripted REST API to create incident along with attachment which could be in any format ( file type, name along extension & base64 encoded string ) provided by 3rd part (.net program). i am unable decode it to base64 and attach it to INC any help on this is welcomed. Thanks in advance .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2024 09:41 AM - edited ‎07-17-2024 09:47 AM
Hi @Prateek1
Try following script
var attachment_name = ;//attachment name with extension
var contentType = ;//content type of the file(optional)
var incident_id = "sys_id of the incident";
var stringUtil = GlideStringUtil;
var attachment = new Attachment();
var value = attachment.write('incident', incident_id, attachment_name, contentType, stringUtil.base64DecodeAsBytes(base64 encoded string));
Thanks,
Swathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2024 09:41 AM - edited ‎07-17-2024 09:47 AM
Hi @Prateek1
Try following script
var attachment_name = ;//attachment name with extension
var contentType = ;//content type of the file(optional)
var incident_id = "sys_id of the incident";
var stringUtil = GlideStringUtil;
var attachment = new Attachment();
var value = attachment.write('incident', incident_id, attachment_name, contentType, stringUtil.base64DecodeAsBytes(base64 encoded string));
Thanks,
Swathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2024 09:50 AM
worked for image/png will test this out and accept as solution . Thanks for quick response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2024 09:33 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2024 09:45 AM
Hi @Prateek1 ,
Sample JSON request:
{
"incidentNumber": "incident number here",
"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);
Mark the comment as a correct answer and also helpful once worked.
Thanks & Regards,
Sumanth Meda