Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to create an incident along with adding an attachment via Rest API post call

srinivasrao
Tera Contributor

Hi Team,

Team,

I wrote a scripted API and trying to post the data from rest API explored, but i dont see provision to add attachement data from explorer to test the case.

May i know how to achive this?

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

// implement resource here
var requestBody = request.body.data;
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();

// Set fields without inserting the record yet
incidentGR.short_description = requestBody.short_description;
incidentGR.caller_id.setDisplayValue(requestBody.caller_id);
incidentGR.short_description = requestBody.short_description;
incidentGR.caller_id.setDisplayValue(requestBody.caller_id);

// Attachments processing (simulated for now)
// Assuming attachments are part of the request payload
var attachmentIDs = [];
if (requestBody.attachments && requestBody.attachments.length > 0) {
for (var i = 0; i < requestBody.attachments.length; i++) {
var attachment = requestBody.attachments[i];
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.initialize();
attachmentGR.table_name = 'incident';
attachmentGR.table_sys_id = incidentGR.sys_id;
attachmentGR.file_name = attachment.file_name;
attachmentGR.content_type = attachment.content_type;
attachmentGR.insert();
attachmentIDs.push(attachmentGR.sys_id);
}
}

// Now insert the incident record
// Return response with incident and attachment info
response.setBody({
sys_id: incidentSysID,
number: incidentGR.number,
attachments: attachmentIDs
});
})(request, response);
17 REPLIES 17

chanakya_mekala
Tera Contributor

We have implemented this, there's a OOB function for this setRequestBodyFromAttachment()

Sample:

var r = new sn_ws.RESTMessageV2(<API/Endpoint>, <method>);
r.setRequestBodyFromAttachment(<attachmentSysId>); // In this case first we have to mention the sys_id of the attachment in sys_attachment table.
var response = r.execute();
var httpStatusCode = response.getStatusCode();

It works smoothly, no need of additional code.


Please mark my response as Answered or Helpful if it had done so

Hi Chankya,

Thanks for the mail. I can try that , but initially the data is not populating to source attachment fields from the Rest API explorer which i am currently having no clue why

 

Regards,

Srinivas

Team,

Finally i am able to create a record and also attachment also getting added. But the added attachement is showing as undefined. 

gs.log is showing filename,filecontenttype and content logged as undefined. 

Below is the script: 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// Implement resource here

var requestBody = request.body.data;
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
gs.log('level1 : ' + requestBody.level1 + 'level2 : ' + requestBody.level2);

// Set fields without inserting the record yet
incidentGR.short_description = requestBody.short_description;
incidentGR.description = requestBody.description;
incidentGR.u_store_id = 'kiko_store';
incidentGR.caller_id.setDisplayValue(requestBody.beneficiary);
incidentGR.category = requestBody.level1;
incidentGR.subcategory = requestBody.level2;
incidentGR.contact_type = 'Site Manager';
incidentGR.state = 1;
incidentGR.impact = 1;
incidentGR.urgency = 3;
incidentGR.assignment_group.setDisplayValue('Streamline Support');
incidentGR.company.setDisplayValue('Kiko Milano');

var incidentSysID = incidentGR.insert();

var attachmentIDs = [];
if (requestBody.attachments && requestBody.attachments.length > 0) {
for (var i = 0; i < requestBody.attachments.length; i++) {
var attachment = requestBody.attachments[i];

var parser = new global.JSON();
var parsedData = parser.decode(attachment.data);
var fileName = parsedData.fileName;
var fileContentType = parsedData.contentType;
var fileData = parsedData.content;
gs.log('filename: ' + fileName + '&&'+parsedData.fileName +'***'+'filecontent type : ' + fileContentType +'&&'+ parsedData.fileContentType +'***'+'filedata : '+fileData+'&&'+parsedData.fileData);

if (incidentSysID) {
var sa = new GlideSysAttachment();
var attachmentSysID = sa.write(incidentGR, fileName, fileContentType, fileData);
attachmentIDs.push(attachmentSysID);
}
}
}

// Return response with incident and attachment info
response.setBody({
sys_id: incidentSysID,
number: incidentGR.number,
caller_id: incidentGR.caller_id.getDisplayValue(),
short_description: incidentGR.short_description,
description: incidentGR.description,
contact_type: incidentGR.contact_type,
company: incidentGR.company.getDisplayValue(),
state: incidentGR.state,
priority: incidentGR.priority,
assignment_group: incidentGR.assignment_group.getDisplayValue(),
category: incidentGR.category,
level1: incidentGR.category,
level2: incidentGR.subcategory,
attachments: attachmentIDs
});

})(request, response);