Populate configuration item on incident form - Inbound Integration

Joshuu
Kilo Sage

Hello All,

 

I am working on an inbound integration in ServiceNow. When I send request from source instance I am able to receive all the field values in the target instance except configuration item field(cmdb_ci). It is blank.

 

Please find the below script that I have added.

 

Source Instance:

Business Rule:

 

 

(function executeRule(current, previous /*null when async*/) {

	var request = new sn_ws.RESTMessageV2('Sending Data Create','Post');

	request.setStringParameterNoEscape('short_description',current.short_description);
	request.setStringParameterNoEscape('description',current.description);
	request.setStringParameterNoEscape('category',current.category);
	request.setStringParameterNoEscape('subcategory',current.subcategory);
	request.setStringParameterNoEscape('impact',current.impact);
	request.setStringParameterNoEscape('urgency',current.urgency);
	request.setStringParameterNoEscape('priority',current.priority);
	request.setStringParameterNoEscape('comments',current.comments);
	request.setStringParameterNoEscape('work_notes',current.work_notes);
	request.setStringParameterNoEscape('number',current.number);
	request.setStringParameterNoEscape('cmdb_ci',current.cmdb_ci);
	request.setStringParameterNoEscape('contact_type',current.contact_type);
	request.setStringParameterNoEscape('assignment_group',current.assignment_group);
	request.setStringParameterNoEscape('close_notes',current.close_notes);
	request.setStringParameterNoEscape('close_code',current.close_code);
	request.setStringParameterNoEscape('state',current.state);
	request.setStringParameterNoEscape('hold_reason',current.hold_reason);	
	
	var response=request.execute();
	var requestBody=request.getRequestBody();
	var responseBody=response.getBody();
	var httpStatus=response.getStatusCode();
	gs.log(responseBody);
	
	responseJSON=responseBody.substring(10,responseBody.length-1);
	parsedJSON=JSON.parse(responseJSON);


})(current, previous);

 

 

HTTP Method:

 

 

{
"state": "${state}",
"hold_reason": "${hold_reason}",
"description":"${description}",
"short_description": "${short_description}",
"category": "${category}",
"impact":"${impact}",
"urgency":"${urgency}",
"priority" :"${priority}",
"subcategory": "${subcategory}",
"cmdb_ci": "${cmdb_ci}",
"contact_type":"${contact_type}",
"item": "${item}",
"assignment_group": "${assignment_group}",
"close_notes": "${close_notes}",
"close_code":"${close_code}",
"work_notes": "${work_notes}",
"comments":"${additional_comments}",
"id":"${number}"
}

 

 

 

Target Instance:

Script:

 

 

var InsertIncident = Class.create();
InsertIncident.prototype = {
    initialize: function() {},

    ProcessInsertIncident: function(requestData, response) {

        var validJSON = this._checkJSONValidity(requestData);
        var reqeustJSONParser = JSON.parse(validJSON);

        var responseBody = {};

        if (validJSON) {

            var inc = new GlideRecord('incident');
            inc.addQuery('correlation_id', reqeustJSONParser.id);
            inc.query();

            if (inc.next()) {
                if (reqeustJSONParser.state == '3') {

                    inc.state = reqeustJSONParser.state;
                    inc.hold_reason = reqeustJSONParser.hold_reason;
                    inc.description = reqeustJSONParser.description;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.category = reqeustJSONParser.category;
                    inc.impact = reqeustJSONParser.impact;
                    inc.urgency = reqeustJSONParser.urgency;
                    inc.cmdb_ci = reqeustJSONParser.cmdb_ci;
                    inc.priority = reqeustJSONParser.priority;
                    inc.subcategory = reqeustJSONParser.subcategory;
                    inc.contact_type = reqeustJSONParser.contact_type;
                    inc.assignment_group = reqeustJSONParser.assignment_group;
                    inc.close_notes = reqeustJSONParser.close_notes;
                    inc.close_code = reqeustJSONParser.close_code;
                    inc.work_notes = reqeustJSONParser.work_notes;
                    inc.comments = reqeustJSONParser.comments;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';
                }

            } else {

                inc.initialize();

                inc.state = reqeustJSONParser.state;
                inc.hold_reason = reqeustJSONParser.hold_reason;
                inc.description = reqeustJSONParser.description;
                inc.short_description = reqeustJSONParser.short_description;
                inc.category = reqeustJSONParser.category;
                inc.impact = reqeustJSONParser.impact;
                inc.urgency = reqeustJSONParser.urgency;
                inc.cmdb_ci = reqeustJSONParser.cmdb_ci;
                inc.priority = reqeustJSONParser.priority;
                inc.subcategory = reqeustJSONParser.subcategory;
                inc.contact_type = reqeustJSONParser.contact_type;
                inc.assignment_group = reqeustJSONParser.assignment_group;
                inc.close_notes = reqeustJSONParser.close_notes;
                inc.close_code = reqeustJSONParser.close_code;
                inc.work_notes = reqeustJSONParser.work_notes;
                inc.comments = reqeustJSONParser.comments;
                inc.correlation_id = reqeustJSONParser.id;

                inc.insert();

                responseBody.Message = 'Incident is Created';
                responseBody.Number = inc.number;

            }

            response.setBody(responseBody);
        }
    },

    _checkJSONValidity: function(validJSON) {
        try {
            return JSON.stringify(validJSON);
        } catch (e) {
            gs.info('parsing error' + e);
        }
    },
    type: 'InsertIncident'
};

 

 

@Mohith Devatte Could you please help me.

 

Thanks.

Priya.

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

If you are receiving Name of CI then you need to make changes like below:

if (inc.next()) {
                if (reqeustJSONParser.state == '3') {

                    inc.state = reqeustJSONParser.state;
                    inc.hold_reason = reqeustJSONParser.hold_reason;
                    inc.description = reqeustJSONParser.description;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.category = reqeustJSONParser.category;
                    inc.impact = reqeustJSONParser.impact;
                    inc.urgency = reqeustJSONParser.urgency;
                   
var ciGr=new GlideRecord('cmdb_ci');
ciGr.addQuery('name',reqeustJSONParser.cmdb_ci);
ciGr.query()
if(ciGr.next){
 inc.cmdb_ci = ciGr.getUniqueValue();
}
                    inc.priority = reqeustJSONParser.priority;
                    inc.subcategory = reqeustJSONParser.subcategory;
                    inc.contact_type = reqeustJSONParser.contact_type;
                    inc.assignment_group = reqeustJSONParser.assignment_group;
                    inc.close_notes = reqeustJSONParser.close_notes;
                    inc.close_code = reqeustJSONParser.close_code;
                    inc.work_notes = reqeustJSONParser.work_notes;
                    inc.comments = reqeustJSONParser.comments;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';
                }

            }

 

Added GlideRecord query in above part, use same logic incase of both insert and update.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

6 REPLIES 6

Harshad Wagh
Tera Guru

is your cmdb_ci sys_id  on source instance matches with target instance?

Thanks

Harshad

Anil Lande
Kilo Patron

Hi,

Have you tried adding logs in your script?

Put log and see what value coming for cmdb_ci attribute. It should be sys_id and you should have record with same sys_id in your instance.

 

If you are receiving name as response instead of sys_id then you need to make GlideRecord query to get CI sys_id matching name of CI  and use it to populate the field.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Anil Lande
Kilo Patron

If you are receiving Name of CI then you need to make changes like below:

if (inc.next()) {
                if (reqeustJSONParser.state == '3') {

                    inc.state = reqeustJSONParser.state;
                    inc.hold_reason = reqeustJSONParser.hold_reason;
                    inc.description = reqeustJSONParser.description;
                    inc.short_description = reqeustJSONParser.short_description;
                    inc.category = reqeustJSONParser.category;
                    inc.impact = reqeustJSONParser.impact;
                    inc.urgency = reqeustJSONParser.urgency;
                   
var ciGr=new GlideRecord('cmdb_ci');
ciGr.addQuery('name',reqeustJSONParser.cmdb_ci);
ciGr.query()
if(ciGr.next){
 inc.cmdb_ci = ciGr.getUniqueValue();
}
                    inc.priority = reqeustJSONParser.priority;
                    inc.subcategory = reqeustJSONParser.subcategory;
                    inc.contact_type = reqeustJSONParser.contact_type;
                    inc.assignment_group = reqeustJSONParser.assignment_group;
                    inc.close_notes = reqeustJSONParser.close_notes;
                    inc.close_code = reqeustJSONParser.close_code;
                    inc.work_notes = reqeustJSONParser.work_notes;
                    inc.comments = reqeustJSONParser.comments;

                    inc.update();

                    responseBody.Message = 'Incident is Updated';
                }

            }

 

Added GlideRecord query in above part, use same logic incase of both insert and update.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

 

Thanks for your response.

 

The Configuration item is not visible on form as below. But I can see the value in the history.

 

priyarao_0-1664865455872.png

 

 

priyarao_1-1664865517080.png

 

And as you suggested I have modified the code by adding the below code. But this time I can't see the ci in the history as well.

var ciGr=new GlideRecord('cmdb_ci');
ciGr.addQuery('name',reqeustJSONParser.cmdb_ci);
ciGr.query()
if(ciGr.next){
 inc.cmdb_ci = ciGr.getUniqueValue();
}

 

Same issue is happening for "Assignment Group" as well. Is this because these two are reference fields ? If yes, how to proceed with this? Also, I can see the CI name in the logs.

 

Please help.

Thanks,

Priya.