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

ShubhamGarg
Kilo Sage

Hello @srinivasrao ,

 

I believe you should consider using Attachment API along-with your scripted rest API to create incident. In your incident payload you are passing metadata for your attachment but not actual attachment itself.

 

Attachment API 

 

Create Atachment in ServiceNow Using Attachment API and OAuth Authorization

 

Hope it helps.

 

Regards,

Shubham

Hi Shubham,

 

Thank for your mail. Client request as below. Both incident and attachment to be created at a time. That is why i am working on import set rather than scripted API.

{
    "caller": "73A",
    "short_description": "Issue with mobile device",
    "description": "Issue with mobile device",
    "level1": "iphone",
    "level2": "Charging",
    "attachments": [
        {
            "fileName": "example.txt",
            "contentType": "text/plain",
            "content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQu"
        }
    ]
}

srinivasrao
Tera Contributor

Hi Subham, 

Thanks for the mail . I am testing this scripted API from Rest api explorer. Giving the below request body.

Request Body
{
"short_description":"Incdent is created from Innovaway",
"caller_id":"73A",
}

{
    "short_description": "Issue with mobile device",
    "caller_id": "73A",
    "attachments": [
        {
            "file_name": "example.txt",
            "content_type": "text/plain",
            "file_content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQu"
        }
    ]
}
The response i am getting:  500 internal error

Somehow i dont see the error , I saw the attachement created in sys_attachment table but its empty , also not  attached to the incident