Business Rule: Create Problem when an incident has 5 child incidents

Brendan Hallida
Kilo Guru

Hi all,

I have a requirement to create a problem record when an incident has 5 child incidents.

I was thinking of using glide aggregate to perform the count, and use the fact that when the 5th child has the parent added, the comments are updated in the parent and then will run the BR.

Here is the script that I have, it is not working correctly.  I feel I am close.

When to Run: Before on Insert and Update

Script:a

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

//Find all child incidents for current incident
var childInc = new GlideAggregate('incident');
childInc.addQuery('parent' , current);
childInc.addQuery('active' , 'true');
childInc.addAggregate('count');
childInc.query();

while(childInc.next()){
gs.log('The number of child incidents for ' + current.number + ' is: ' + childInc.getAggregate('count'));

if(childInc.getAggregate('count') >=5) {
gs.log('>=5 worked');

    // Create the problem
	var problem = new GlideRecord('problem');
	problem.short_description = current.short_description;
	problem.description = current.description;
	problem.cmdb_ci = current.cmdb_ci;
	problem.priority = current.priority;
	problem.assignment_group = current.assignment_group;
	problem.assigned_to = current.assigned_to;
	problem.category = current.category;
	problem.subcategory = current.subcategory;
	problem.impact = current.impact;
	problem.urgency = current.urgency;
	problem.business_service = current.business_service;
	problem.first_reported_by_task = current.sys_id;
	
	problem.insert();
	gs.log('Problem Number is: ' + problem.number);
	// Link the problem to this incident
	current.problem_id = problem.sys_id;
	current.setWorkflow(false);
    current.work_notes = 'A problem: ' + problem.number + ' has been created as this incident contains more than 5 child incidents.';
	current.update();
	
}

else{
gs.log('>=5 failed');
}
}   
})(current, previous);

Does anyone have any ideas?  the problem creation part I'm not to fussed with and probably needs refining, I cannot get the logic to work.

Cheers, Brendan

12 REPLIES 12

Hey Mark,

The field is out of the box in Madrid with problem best practice

find_real_file.png

asifnoor
Kilo Patron

Hi,

Instead of sayuing current, say current.sys_id in addquery.

Then, in your condition, just mention

if(childInc.getAggregate('count') ==5) //so that it runs only when 5 incidnets are created and does not turn when 6 or 7 incidents are created.

Later, in the probem, do add initialize as Abhishek suggested earlier.

Mark the comment as a correct answer and helpful if it helps.

 

Raghav Kapoor
Tera Contributor

Write After Business Rule --> Insert/Update

 

var childInc = new GlideRecord('incident');
    childInc.get(current.sys_id);
    childInc.addEncodedQuery('active=true^child_incidents=5');
    childInc.query();
    if (childInc.next()) {

            // Create the problem
            var problem = new GlideRecord('problem');
            problem.initialize();
            problem.short_description = current.short_description;
            problem.description = current.description;
            problem.cmdb_ci = current.cmdb_ci;

            problem.insert();
            current.problem_id = problem.sys_id;
            current.setWorkflow(false);
            current.work_notes = "A problem: " + problem.number + "has been created as child Incident = 5.";
            current.update();
        }