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

I am not doing it at the parent level, but the task.   When you update a task, it checks the parent, looks at the end date, makes sure it is still valid after the change of the end date of the child task.


damodarreddyp
ServiceNow Employee
ServiceNow Employee

These scripts will run for planned_task related tables [which actually installed with Project Management plugin Planned task ] and not on TASK table.



Ideally, if you are extending Table B from Table A:


1. end_date column is created on Table A, then it should be available on Table B


2. as the column is derived from parent table if you enter a value on Table B, it should be automatically reflecting on Table A and also on TASK



OR



If you created different columns on each table, then you have to identify the relationship between tables and update the respective parent table columns.



Hope i got your question clearly, correct me if am missing something here!


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