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

@Shiva Kumar8 

did you give correct sysId?

share the script here

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

Hi Ankur,

 

Yes I provided the correct sysid, please find 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);
        incident.assignment_group = '0ae2acefdbce0f0067e1dd3b5e96199b';
        // Add comment to incident item
        incident['work_notes'].setJournalEntry(request.body.data.message);
        // Update the incident item
        incident.assignment_group = '0ae2acefdbce0f0067e1dd3b5e96199b'; // give here the group sysId here
        incident.cmdb_ci = '171b347cdb3e3bc067e1dd3b5e9619a8'; // 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);

Hi @Ankur Bawiskar I provided the correct SysId still not working

@Shiva Kumar8 

is the work notes getting update?

are you sure the sysId of incident is correct in the get method?

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