- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 08:20 AM
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?
Solved! Go to Solution.
- Labels:
-
Customer Service Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 10:55 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 08:46 AM
Hi,
Create a After insert business rule on child table.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var count = new GlideAggregate('incident_task');//replace your table name
count.addAggregate('COUNT');
count.addQuery('incident', current.incident); //
count.query();
var child_incidents = 0;
if (count.next())
child_incidents = count.getAggregate('COUNT');
current.u_task_count = child_incidents;//replace your table name
current.update();
})(current, previous);
Please mark it as helpful (or) correct if it helps
Thanks,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 08:51 AM
you should avoid current.update() in business rule,
also if you are setting the value in current object , go with before business rule.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 09:19 AM
Also if it is a child case it would not have a child table. It would be on the same table the parent fields just gets updated.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2020 10:55 AM
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);