creating an incident using Scripted rest Api

Shiva Kumar8
Kilo Guru

Hi community,

 

I'm creating an incident using rest API by integrating servicenow with hackerone, I was able to map few fields but few fields like assignment group and configuration item needs to be hard coded can some one please help me get this wowk I'm sharing the script below

 

 

 

(
    function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
        // Retrieve the incident with the passed in sys_id
        var sys_id = request.body.data.sys_id;
        // Create a new record for the table you want to use (in our example `incident`)
        var incident = new GlideRecord('incident');
        incident.get(sys_id);
        // Add comment to incident item
        incident['work_notes'].setJournalEntry(request.body.data.message);
        // Update the incident item
        incident.update();
        // Retrieve the last added comment on this incident
        var comment = new GlideRecord('sys_journal_field');
        comment.addQuery('element_id', sys_id);
        comment.addQuery('name', 'incident');
        comment.addQuery('element', 'work_notes');
        comment.addQuery('value', request.body.data.message);
        comment.orderByDesc('sys_created_on');
        comment.setLimit(1);
        comment.query();
        if (comment.next()) {
            //Store last comment in variable
            var last_comment = comment;
        }
        response.setBody({
            'sys_id': incident.sys_id,
            'comment_sys_id': last_comment.sys_id,
            'comment_value': last_comment.value,
            'request_sys_id': request.body.data.sys_id,
            'request_message': request.body.data.message
        });
    }
)(request, response);

 

 

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@Shiva Kumar8 

in your script you are not updating group or ci

where is that script?

what's your exact business requirement? you want to update INC or return data for INC

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Thanks for the reply, I'm not sure where to add the piece of code to update the group or ci can you please help that

@Shiva Kumar8 

if you want to hard-code group and CI then you can add these lines in bold

(
function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// Retrieve the incident with the passed in sys_id
var sys_id = request.body.data.sys_id;
// Create a new record for the table you want to use (in our example `incident`)
var incident = new GlideRecord('incident');
incident.get(sys_id);
// Add comment to incident item
incident['work_notes'].setJournalEntry(request.body.data.message);
// Update the incident item

incident.assignment_group = 'groupSysId'; // give here the group sysId here
incident.cmdb_ci = 'ciSysId'; // give here the CI sysId

incident.update();
// Retrieve the last added comment on this incident
var comment = new GlideRecord('sys_journal_field');
comment.addQuery('element_id', sys_id);
comment.addQuery('name', 'incident');
comment.addQuery('element', 'work_notes');
comment.addQuery('value', request.body.data.message);
comment.orderByDesc('sys_created_on');
comment.setLimit(1);
comment.query();
if (comment.next()) {
//Store last comment in variable
var last_comment = comment;
}
response.setBody({
'sys_id': incident.sys_id,
'comment_sys_id': last_comment.sys_id,
'comment_value': last_comment.value,
'request_sys_id': request.body.data.sys_id,
'request_message': request.body.data.message
});
}
)(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

I added the script but it is still not populating the fields