How can you create a counter to count the number of children a Major Case has?

zag
Tera Expert

I created a field called Child Count in the MIM scope. 

Basically I want a business rule that add's one every time a Child is Added or Subtracts one if a child is removed. 

Basically I want the Child Case related list counted. Any ideas?

 

 

 

1 ACCEPTED SOLUTION

Brian Lancaster
Tera Sage

I tested this in my PDI.  It is an after insert/update business rule which is one of the occasions where you can have an update() in the BR.  In the when to run tab I have my condition as parent changes this way it can update the count if you remove a child case because it was added by accident.  Then I have the following code.

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

    // Add your code here
	var sysID = '';
	if (current.getDisplayValue('parent') == ''){ //update count if case removed
		sysID = previous.getValue('parent');
	}
	else { //update count if case added
		sysID = current.getValue('parent');
	}
	
    var gr = new GlideRecord('sn_customerservice_case');
    gr.addQuery('sys_id', sysID);
    gr.query();
    if (gr.next()) {
        gr.u_child_count = getCount(sysID);//get the count thought the below function
		gr.update();
    }

	//function to get count thought GlideAggregate as using glide record to count can cause pour performance.
    function getCount(sysID) {
        var count = new GlideAggregate('sn_customerservice_case');
        count.addQuery('parent', sysID);
        count.addAggregate('COUNT');
        count.query();
        if (count.next()) {
            return count.getAggregate('COUNT');
        }
    }

})(current, previous);

View solution in original post

11 REPLIES 11

Thanks this is really helpful but the problem is when we delete the child case. Still looking for a way to solve that

zag
Tera Expert

Dude!! thanks !! works great. 

zag
Tera Expert

@Brian Lancaster  Hey, I noticed that this only works for new adds or removes , how would I initialize all the records?

For the cases that are not closed you could remove one of the child cases and add it back.  If you want historical data on close cases we could convert the script I gave you and run it in the scripts background.

Yea there are too many of them to do manually...so I need to groom at least the Open ones.

 

This is what I have... 

 

When to Run:

Major Case State = Accepted

State  Is Not One Of  Cancelled or Closed

 

Advanced

 

var sysID = '';

var gr = new GlideRecord('sn_customerservice_case');
gr.addQuery('sys_id', sysID);
gr.query();
if (gr.next()) {
gr.u_child_count = getCount(sysID);//get the count thought the below function
gr.update();
}

//function to get count
function getCount(sysID) {
var count = new GlideAggregate('sn_customerservice_case');
count.addQuery('parent', sysID);
count.addAggregate('COUNT');
count.query();
if (count.next()) {
return count.getAggregate('COUNT');
}
}

 

Is it this simple?