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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

You could simplify your code. The GlideAggregate doesn't need a while for example. The addQuery for active, use true without quotes (it's a boolean) or use addActiveQuery.

The actual problem creation, the comment from Abhisnek is very useful.

Question:
When does this BR run. I mean, this occurs if a 5th Incident is created. Though where is your logic from preventing this to run if a 6th or 7th incident is created? Guess you don't want new problems each time this happens?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Cheers mark, will look into simplifying the code 🙂

Good call out with the extra child incidents, I was planning on using a condition around ignoring if the incidents problem_id is not empty

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi Brendan,

 

I would have a different approach here. First I would use a flow, so you don't need to write any code at all =). Then there is actually already a counter on incidents that shows how many child incidents it has. It's called "child incidents".

So you can bascially just have a trigger saying that when that field is 5 or higher and doesn't have a value in the problem field, run this. If it's a flow, you can even make it as a run once, so it won't even check the trigger later on.

Then if it's triggered, you just need to create the problem and update the problem field on the incident.

How does that sound?

//Göran
Feel free to connect:
LinkedIn
Subscribe to my YouTube Channel
or look at my Book: The Witch Doctor’s Guide To ServiceNow

Hi Göran,

Sweet, I will take a look.  I did notice that the child.incidents field is not filling in my development instance.

find_real_file.png

Mark Roethof
Tera Patron
Tera Patron

Other thing I noticed:
Are all problem fields correct? You are using for example problem.first_reported_by_task. I don't see this out-of-the-box? Should this be a u_ field?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn