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.

Scripted Rest API call sending Attachment along with creation of incident data is not working

srinivasrao
Tera Contributor

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);
***************************************
I send the below request body from Rest API explorer , but attachement is empty after creaing incident .
{
    "short_description": "Issue with mobile device",
    "caller_id": "73A",
    "attachments": [
        {
            "file_name": "example.txt",
            "content_type": "text/plain",
            "file_content": "VGhpcyBpcyBhIHNhbXBsZSBmaWxlIGNvbnRlbnQu"
        }
    ]
}
 
2 REPLIES 2

Allen Andreas
Tera Patron

Duplicate post, please don't spam the forums: https://www.servicenow.com/community/developer-forum/how-to-create-an-incident-along-with-adding-an-... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Thank you Allen, Tried to find related post from your blog , but dont find it.

Using below 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: 

(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.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('XXXXX Support');
incidentGR.company.setDisplayValue('XXXXXX');

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;

if (incidentSysID) {
var sa = new GlideSysAttachment();
var attachmentSysID = sa.write(incidentSysID, 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);
********************************************************************************
My Request Body is as below: 
"beneficiary": "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"
}
]
}