Setting the min and max values obtained from child object and setting them to parent object

erniec
Giga Expert

Good morning,

I am trying to replicate the List Calculation functionality in determining the min and max values for a set of date fields on this object and placing those values on the parent record. The child object contains a date field on each row and accurately calculates the min and max values when List Calculation is turned on. I would like to have these two values copied to the parent object in respective fields.   Could someone point me in the direction of leveraging the functionality that already exist to retrieve these values.

Thanks in advance.

Ernie

1 ACCEPTED SOLUTION

Mike Allen
Mega Sage

I think the best way to do this would be to write a business rule on the child record that says:



condition: current.child_date.changes()



var parent = new GlideRecord('parent_table');


parent.get(current.parent);



if(gs.dateDiff(current.child_date.getDisplayValue(), parent.date.getDisplayValue(), true) < 0){


        parent.date = current.child_date;


        parent.update();


}


View solution in original post

4 REPLIES 4

Mike Allen
Mega Sage

I think the best way to do this would be to write a business rule on the child record that says:



condition: current.child_date.changes()



var parent = new GlideRecord('parent_table');


parent.get(current.parent);



if(gs.dateDiff(current.child_date.getDisplayValue(), parent.date.getDisplayValue(), true) < 0){


        parent.date = current.child_date;


        parent.update();


}


Thank you so much. This is exactly what I was looking for. I really appreciate it Mike


Steve McCarty
Mega Guru

Ernie,



I would recommend looking into GlideAggregate - ServiceNow Wiki .   I would create an after (or even asynch) business rule on your child table that runs whenever the date field changes.   It would do something like this:



var agg = new GlideAggregate('child_table');


agg.addQuery('parent_field',current.parent_field);


agg.addAggregate('MIN','date_field');


agg.addAggregate('MAX','date_field');


agg.query();


if(agg.next())


{


current.parent_field.max_field = agg.getAggregate('MAX');


current.parent_field.min_field = agg.getAggregate('MIN');


current.parent.update();


}



You may have to get an explicit glideRecord object for the parent to do the update and you could also check the values to see if they have changed so you don't have to update the parent if they haven't.   Hopefully that gets you going in   a good direction.



-Steve


Thanks for this info Stephen. I will keep this in my back pocket for another task that I have to complete.



Thanks again for your help