To Create a Major Incident

M Lohith Datta
Tera Contributor

Hi, My use case is that when there are 200 tickets with the same Location I need to create one Major Incident and attach all these 200 tickets as Child Incidents to this newly created Major Incident.

I have tried the below code but the problem I am facing is that more than 20  Major incidents are getting created when I run this code and also I am not getting any Logic on how to link those 200 tickets to this new Major incident.

After BR with Insert and Update:

(function executeRule(current, previous /*null when async*/ ) {
var ga = new GlideAggregate("incident");
ga.addQuery("location", current.location);
ga.addQuery("state", "<", "6");
ga.addQuery("major_incident_state","=","");
ga.addAggregate("COUNT");
ga.query();
while (ga.next()) {
var count = ga.getAggregate("COUNT");

}
gs.log("MLD Count" + count);


if(count > 5) {
var gr = new GlideRecord("incident");
gr.initialize();
gr.major_incident_state = "Accepted";
gr.short_description = "Major incident is created for 5 tickets with location :" + current.location;
gr.caller_id = "6816f79cc0a8016401c5a33be04be441";
gr.insert();


}

})(current, previous);

*for now I am comparing it with 5 and also MI should copy as many fields it can from latest created incident\

Kindly help me with this Use case

Thanks, 

M Lohith Datta

1 ACCEPTED SOLUTION

suvro
Mega Sage
Mega Sage

OR you can try below code

 

var majInSys = "nothing";
var gaInc = new GlideAggregate('incident');
 gaInc.addActiveQuery();
 gaInc.addQuery('location', current.location);
 gaInc.addAggregate('COUNT');
 gaInc.orderByDesc('created');
 gaInc.query();
 
 if (gaInc.next()){
 
 var count = gaInc.getAggregate('COUNT');
 if (count == 200){
    var gr = new GlideRecord("incident");
gr.initialize();
gr.major_incident_state = "Accepted";
gr.short_description = "Major incident is created for 5 tickets with location :" + current.location;
gr.caller_id = "6816f79cc0a8016401c5a33be04be441";
gr.field = gaInc.field;
gr.field = gaInc.field;
gr.field = gaInc.field;
majInSys = gr.insert();
 }
 }
 if (majInSys != "nothing"){
 var grIncU = new GlideRecord('incident');
 grIncU.addActiveQuery();
 grIncU.addQuery('location',current.location);
 grIncU.query();
 
 
 while (grIncU.next()){
 
 grIncU.parent = majInSys;
 grIncU.setWorkflow(false);
 grIncU.update();
 }
 }

View solution in original post

4 REPLIES 4

suvro
Mega Sage
Mega Sage

Hi Lohith,

 

Why not create a field in location record which stores integer like number_of_incidents.

Whenver an Incident is created, update its location record with number_of_incidents = number_of_incidents +1.

Whenver an Incident is closed , update its location record with number_of_incidents = number_of_incidents - 1.

Write a BR in location table which runs whenver number_of_incident changes. If the count is 200. Create a major incident. Capture its sys_id.

Then GlideRecord the incident table, and query all active incidents having that location. update their parent field with sys_id of the major incident.

 

This is a very simplified process.

Hi Suvro,

Thanks for the simplified explanation but in my case, I want to proceed without creating any new fields and also I wanna know why my above code is not working properly.

Thanks,

Datta

replace while with if only one major incident will be created. Plus I have shared the whole code with you. You can compare and implement

suvro
Mega Sage
Mega Sage

OR you can try below code

 

var majInSys = "nothing";
var gaInc = new GlideAggregate('incident');
 gaInc.addActiveQuery();
 gaInc.addQuery('location', current.location);
 gaInc.addAggregate('COUNT');
 gaInc.orderByDesc('created');
 gaInc.query();
 
 if (gaInc.next()){
 
 var count = gaInc.getAggregate('COUNT');
 if (count == 200){
    var gr = new GlideRecord("incident");
gr.initialize();
gr.major_incident_state = "Accepted";
gr.short_description = "Major incident is created for 5 tickets with location :" + current.location;
gr.caller_id = "6816f79cc0a8016401c5a33be04be441";
gr.field = gaInc.field;
gr.field = gaInc.field;
gr.field = gaInc.field;
majInSys = gr.insert();
 }
 }
 if (majInSys != "nothing"){
 var grIncU = new GlideRecord('incident');
 grIncU.addActiveQuery();
 grIncU.addQuery('location',current.location);
 grIncU.query();
 
 
 while (grIncU.next()){
 
 grIncU.parent = majInSys;
 grIncU.setWorkflow(false);
 grIncU.update();
 }
 }