The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Add all incident to problem related list

pvv1045330
Tera Contributor

Hi Team,

I have a requirement below. 

Create a problem ticket from incident if incident has been created more than 5 time for same CI. and all incident will be add to under problem related list, at that same time all incident records display the problem_id on incident table.

 

 in this below code how to add incident to problem related list and how to display the problem_id on incidents records? 

 

Have create a after BR on incident table.

 

var ci = new GlideAggregate('incident');
ci.addAggregate('COUNT');
ci.addQuery('cmdb_ci', current.cmdb_ci);
ci.query();
var count = 0;
if (ci.next()) {
count = ci.getAggregate('COUNT');
if (count==5) {
var prb = new GlideRecord('problem');
prb.initialize();
prb.short_description = "Creating this Problem as more than 5 Incidents are created on the Configuration item" + current.cmdb_ci.getDisplayValue()+' '+ current.short_description;

prb.state = 'Open';
prb.insert();
}
current.problem_id=prb.sys_id;
current.update();
}

 

1 ACCEPTED SOLUTION

SUBHAM AGARWAL
Tera Guru

Hello,

You can try this below code. Its working but the code is bit long try to minimize it

 

 

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

// Add your code here
var ci = new GlideAggregate('incident');
ci.addAggregate('COUNT');
ci.addQuery('cmdb_ci', current.cmdb_ci);
ci.addNullQuery('problem_id');
ci.query();
var count = 0;
if (ci.next()) {
count = ci.getAggregate('COUNT');
}
if (count >= 2) {
var prb = new GlideRecord('problem');
prb.initialize();
prb.short_description = "Creating this Problem as more than 5 Incidents are created on the Configuration item" + current.cmdb_ci.getDisplayValue() + ' ' + current.short_description;
var sysId = prb.insert(); //storing sys_id of problem record
current.problem_id = sysId;// updating the current record as it will not come in gliderecord as its not inserted yet
var gr = new GlideRecord('incident');
gr.addQuery('cmdb_ci', current.cmdb_ci);
gr.addNullQuery('problem_id');
gr.query();
while (gr.next()) {

gr.problem_id = sysId;
gr.update();
}


}

})(current, previous);

View solution in original post

6 REPLIES 6

SUBHAM AGARWAL
Tera Guru

Hello,

You can try this below code. Its working but the code is bit long try to minimize it

 

 

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

// Add your code here
var ci = new GlideAggregate('incident');
ci.addAggregate('COUNT');
ci.addQuery('cmdb_ci', current.cmdb_ci);
ci.addNullQuery('problem_id');
ci.query();
var count = 0;
if (ci.next()) {
count = ci.getAggregate('COUNT');
}
if (count >= 2) {
var prb = new GlideRecord('problem');
prb.initialize();
prb.short_description = "Creating this Problem as more than 5 Incidents are created on the Configuration item" + current.cmdb_ci.getDisplayValue() + ' ' + current.short_description;
var sysId = prb.insert(); //storing sys_id of problem record
current.problem_id = sysId;// updating the current record as it will not come in gliderecord as its not inserted yet
var gr = new GlideRecord('incident');
gr.addQuery('cmdb_ci', current.cmdb_ci);
gr.addNullQuery('problem_id');
gr.query();
while (gr.next()) {

gr.problem_id = sysId;
gr.update();
}


}

})(current, previous);

Hi Subham,

 

Thanks for the Replay, it's working as excepted but facing one issue.

if 1st and 2nd incidents created for same CI name problem gets created and add incident under the problem, problem_id value also displayed, but when we created the 3rd,4th,5th,etc..incidents for same CI, all incidents will be add to same problem related list, (which get created when 2nd incident created), as well problem id value should be display.

 

How can overcome this issue?

 

Thanks,

Pavan Kumar

SUBHAM AGARWAL
Tera Guru

Hello,

Change the count>=5 for testing purpose i used 2. 

Thanks

Hi Subham,

 

if(count>=5) problem gets created and all incident are shows under problem ticket, if anther incident created i.e, 6th,7th,8th,etc..... with same CI, all incidents should be added to under same problem ticket(which get created 5th incident creation).

 

Thanks,

Pavan Kumar