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.

How to add incident to Ci's Incident related list if the ci is added in the outages

MS17
Tera Contributor

Hi All,

->I need to add incident to the Ci's Incident related list, if the ci is added in the outages related list. If I add ci to outages, in that ci's related list I want to add the Incident number. 

-> In the below images "*BOW-IBM" is added in Outages related list, I want to add the incident "INC0009009" to "*BOW-IBM" ci's incident related list.

Can anyone please suggest. how to achieve this?

 

rl.PNG

 

rl2.PNG

1 ACCEPTED SOLUTION

I was thinking something more like this for the after Insert Business Rule on the cmdb_ci_outage table:

(function executeRule(current, previous /*null when async*/ ) {
	var affci = new GlideRecord ('task_ci');
	affci.addQuery('ci_item', current.cmdb_ci);
	affci.addQuery('task', current.task_number);
	affci.query();
	if (!affci.hasnext()) {
		affci.initialize();
		affci.ci_item = current.cmdb_ci;
		affci.task = current.task_number;
		affci.insert();
	}
})(current, previous);

So on an incident, when an Outage record is created, you will now see a new record on the Affected CIs Related List on that same Incident with the CI used in the Outage - provided it wasn't already listed as an Affected CI, and the Configuration item field on the Incident remains unaffected.

 

Next create a Relationship.  Name it whatever you want to appear on the tab.  Applies to table = cmdb_ci.  Queries from table = incident.  Query with

(function refineQuery(current, parent) {
	var incArr = [];
	var affci = new GlideRecord('task_ci');
	affci.addQuery('ci_item', parent.sys_id);
	affci.query();
	while (affci.next()) {
		incArr.push(affci.task.toString());
	}
	
	current.addQuery('sys_id', 'IN', incArr.join(','));
})(current, parent);

Once you add this new Related List to the CI form(s) you will see all of the incidents that have an Outage, and hence an Affected CI - or were otherwise added to the Affected CI Related List.

 

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

You could start with an after Insert Business Rule on the cmdb_ci_outage table, that has a script to update the incident record for the current.task_number.  Where this approach falls apart is how it needs to update the incident record.  The OOB Incidents Related List on the cmdb_ci_* table shows incidents that have this CI in the Configuration item field. So in your scenario when BOW-IBM is added in the Outages Related List, the new BR would need to update the Configuration item field on the related Incident to BOW-IBM - where it probably already had BETH-IBM.  I don't think you want to do that, so it would be better if the Business Rule script adds a record to the Affected CIs related list, on which there is already one record for the CI that's populated in the Configuration item field.  Then you would need to create a new (Relationship) Related List that Queries from the task_ci table to show records that match the ci_item field on this table - so you would see all of the incidents where one of the affected CIs is this configuration item record.  Give that a shot and let me know if you get stuck on the BR script or the relationship.

Hi Brad,

Thanks for your reply. I have started with creating after insert/update BR on incident table. I have written below script. I am not much familiar with the scripting part. can you please help me?

After Insert/Update BR:

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

var outageArr = [];
var outGR = new GlideRecord('cmdb_ci_outage');
outGR.addQuery('task_number', current.sys_id);
outGR.query();
while (outGR.next()) {
outageArr.push(outGR.cmdb_ci.sys_id.toString());
}
gs.log("out array" + outageArr);

for (var i = 0; i < outageArr.length; i++) {
var rec = new GlideRecord('cmdb_ci_service');
rec.addQuery('sys_id', outageArr[i]);
//rec.addQuery('ci_item', current.cmdb_ci);
rec.query();
if (rec.next()) {
gs.log("cmdb service" + rec.sys_id);
var ci_service = new GlideRecord('incident.cmdb_ci');
ci_service.initialize();
ci_service.task_number = current.sys_id;
ci_service.insert();
}
}

})(current, previous);

I was thinking something more like this for the after Insert Business Rule on the cmdb_ci_outage table:

(function executeRule(current, previous /*null when async*/ ) {
	var affci = new GlideRecord ('task_ci');
	affci.addQuery('ci_item', current.cmdb_ci);
	affci.addQuery('task', current.task_number);
	affci.query();
	if (!affci.hasnext()) {
		affci.initialize();
		affci.ci_item = current.cmdb_ci;
		affci.task = current.task_number;
		affci.insert();
	}
})(current, previous);

So on an incident, when an Outage record is created, you will now see a new record on the Affected CIs Related List on that same Incident with the CI used in the Outage - provided it wasn't already listed as an Affected CI, and the Configuration item field on the Incident remains unaffected.

 

Next create a Relationship.  Name it whatever you want to appear on the tab.  Applies to table = cmdb_ci.  Queries from table = incident.  Query with

(function refineQuery(current, parent) {
	var incArr = [];
	var affci = new GlideRecord('task_ci');
	affci.addQuery('ci_item', parent.sys_id);
	affci.query();
	while (affci.next()) {
		incArr.push(affci.task.toString());
	}
	
	current.addQuery('sys_id', 'IN', incArr.join(','));
})(current, parent);

Once you add this new Related List to the CI form(s) you will see all of the incidents that have an Outage, and hence an Affected CI - or were otherwise added to the Affected CI Related List.

 

Hi Brad,

-> I have tried the above code, it is working as you said. We need to add the new related list to all CI  forms right?

-> Can we apply this logic in OOTB related list "Incidents", without creating new related list. Is it possible?

Thanks.