Update Parent Task End Date with Child Task End Date?

Edwin Fuller
Tera Guru

I have two custom tables, table A and table B both extended from the task table and Table B is the child of table A.

Whenever the end_date is updated for any of the child task I need the parent end date to reflect the same. If there are multiple child task it should query them all and set the end_date on the parent table to the end date that is the furthers away from today's date. I did some research and I found the below business rule "Recalculate" yet I am not sure what it is doing. Can someone help to recreate this functionality for my custom tables within an scoped application?

Business Rule - Recalculate

Run after insert and update

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

var taskExclusions = new PlannedTaskRecalculationExclusion();

if( !taskExclusions.isExcluded(current)) {

var plannedTaskAPI = new SNC.PlannedTaskAPI();

var recalculationConstraintsStrategy = new RecalculationConstraintsStrategy();

plannedTaskAPI.setConstraints(recalculationConstraintsStrategy.recalculationConstraint(current.top_task.getRefRecord()));

plannedTaskAPI.recalculateTask(current, true);

PostEngineHandlers.fire(current.top_task);

}

})(current, previous);

1 ACCEPTED SOLUTION

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Edwin,



The below script should solve your use case. The algorithm is to find the Child task's End Date which is furthest of all the children (of the current child's parent) and update the parent.



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




var outputDate = current.end_date;


var children = new GlideRecord('table_b');


children.addQuery('table_a', current.table_a);


children.addQuery('sys_id', '!=', current.sys_id);


children.query();


while(children.next()){


if(outputDate < children.end_date)


outputDate = children.end_date;


}



var parentRec = new GlideRecord('table_a');


if(parentRec.get(current.table_a)){


parentRec.end_date = outputDate;


parentRec.update();


}




})(current, previous);




It can be a before or after business rule, but should run on both Insert and Update.



find_real_file.png




Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


View solution in original post

7 REPLIES 7

Mike Allen
Mega Sage

Why don't you just do a business rule akin to:


update of end date on child task


if(current.end_date.compareTo(current.parent.end_date) == -1){   // later than


        var parent = new GlideRecord('parent_table');


        parent.get(current.parent);


        parent.end_date.setValue(current.end_date);


        parent.update();


}


I tried your solution and doesn't seem to be working




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




if(current.end_date.compareTo(current.parent.expected_completion_date) == -1){   // later than


        var parent = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');


        parent.get(current.parent);


        parent.expected_completion_date.setValue(current.end_date);


        parent.update();


}




})(current, previous);


Just try a straight set instead of the setValue and see what that does:



parent.expected_completion_date = current.end_date;


Also this will only work if there was only one child task. I need to query all child task that has an end date populated   and adjust the parent end_date to the date the is furthest out.