Business rule "Recalculate" explanation?

Edwin Fuller
Tera Guru

Can someone explain to me what the below script is doing? I've created a custom table extended from the task table that I'm using as a child table for another table. I need to create a business rule that will recalculate the all of the end dates for each child task the parent task has and set the expected completion date for the parent task to the end date of that child task that is furthest out. I see the planned_task table has the below business rule that recalculates the planned dates, I'm just not sure what exactly it is doing since I've never did this before.

Business Rule: Recalculate

(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

Your script will change as follows. But dont forget to make it as 'After' business rule.



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




var outputDate;


var children = new GlideRecord('x_hemas_connectus2_task');


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


children.orderByDesc('end_date');  


children.query();


if(children.next()){


outputDate = children.end_date;


}


gs.addInfoMessage(' outputDate : ' + outputDate);


var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');


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


gs.addInfoMessage(' outputDate2 : ' + outputDate);


parentRec.expected_completion_date   = outputDate;


parentRec.update();


}




})(current, previous);



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



Thanks


Antin


View solution in original post

11 REPLIES 11

antin_s
ServiceNow Employee
ServiceNow Employee

Glad, it worked for you!


Thanks for your help - I do have one additional update that I need to make to the script.



Currently, it sets the parent "expected_completion_date" field whenever any of the child task planned end date is populated. Is it possible to have the script not make an update until all child task have an end date populated?




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



var gr= new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');


gr.addQuery('sys_id', current.parent);


gr.query();


if(gr.next()){


var st = gr.submission_type;




if (st == 'Change Request') {


var outputDate;    


var children = new GlideRecord('x_hemas_connectus2_task');    


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


children.addQuery('active', true);


children.orderByDesc('end_date');      


children.query();    


if(children.next()){    


outputDate = children.end_date;    


}    


//gs.addInfoMessage(' outputDate : ' + outputDate);    


var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');    


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


//gs.addInfoMessage(' outputDate2 : ' + outputDate);    


parentRec.expected_completion_date   = outputDate;    


parentRec.update();    


}}    


 


 


}})(current, previous);