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

Try adding some logging at the beginning of your script:

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    log.info('Starting onAfter script...');

    var fields = ['u_content', 'u_file_name', 'u_content_type'];

    // Loop through each field
    for (var i = 0; i < fields.length; i++) {
        var fieldName = fields[i];
        var fieldValue = source[fieldName];

        // Check if the field value is empty (null or undefined)
        if (gs.nil(fieldValue)) {
            log.info(fieldName + ' is empty.');
        } else {
            log.info(fieldName + ' is not empty. Value: ' + fieldValue);
        }
    }
...

Hi @Sheldon  Swift ,

Added the code, but don't see anything in logs. onAfter seems not triggering.

 

Regards,

Srinivas

Hi @Sheldon  Swift ,

 

I have tried with gs. log and found the log files , here are they.

 

u_content_type is empty.

u_content is empty.

u_file_name is empty.

Attachment data is incomplete. Skipping attachment.

 

I am sending the data from rest api explorer. But not pulling into transform map.

Regards,

Srinivas

Any help please, nearing my deadline

 

Regards,

Srinivas

Allen Andreas
Tera Patron