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

AbhishekGardade
Giga Sage

Hello Brenden,

1. Check out the modified line :

var childInc = new GlideAggregate('incident');

childInc.addQuery('parent' , current.sys_id);

2. Add below line:

var problem = new GlideRecord('problem');

problem.initialize();

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade

Cheers Abhishek,

Changing the query to current.sys_id is working.  I knew I was close!  Cheers with the problem.initialize() 🙂

Is it working now? Can you mark as correct answer if that helps. Thanks, Abhishek
Thank you,
Abhishek Gardade

The query works, however, the BR doesn't run.

My thought process was that when the child gets attached to the parent, the parent incident is updated with comments - thus the parent incident is updated, so the BR will run when the 5th child is attached.  

But the BR is not running when the comments are added to the parent incident.  Is there anyway to get around this or some other way to get the BR to run on the incident?