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.

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

Hello,

 

This will work now.

 

(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 sysid = '';
var prb = new GlideRecord('problem');
prb.addQuery("short_description", "CONTAINS", current.cmdb_ci.getDisplayValue());
prb.query();
if (prb.next()) {
sysid = prb.sys_id;

} else {
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;
sysid = prb.insert();
}
current.problem_id = sysid;
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);

 

 

Thanks,

Subham

Hi Subham,

 

Prefect, it's working as expected.

 

Thank you very munch for the response.

 

Thanks,

Pavan Kumar