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

You would need to add some logs to see whats going on.



1. Add a log after the while loop to see what value 'outputDate' has (at line # 13 in my script)   - 'gs.log(' outputDate : ' + outputDate);'



2. Which child record are you updating?



3. Add some logs at line # 16 in my code to see if it comes inside the 'if' loop and what value it has.


I am updating "CUTASK01189" in the below screen shot from 11-26-2017 to 11-15-2017 which should update the parent due date to 12-03-2017 seeing that is the furthest date out in the list below



find_real_file.png


When I updated the record to 11-15-1017, it set the parent due date to 11-01-2017 instead of 12-03-2017


find_real_file.pngfind_real_file.png



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


 


 


var outputDate = current.end_date;  


var children = new GlideRecord('x_hemas_connectus2_task');  


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


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


children.query();  


while(children.next()){  


if(outputDate < children.end_date)  


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(' outputDate : ' + outputDate);  


parentRec.expected_completion_date   = outputDate;  


parentRec.update();  


}  


 


 


})(current, previous);  


antin_s
ServiceNow Employee
ServiceNow Employee

Please change the Business Rule to run 'after' ( not before) and use the updated script below. It works for me perfectly.



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




var outputDate;


var children = new GlideRecord('table_b');


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


children.orderByDesc('end_date');


children.query();


if(children.next()){


outputDate = children.end_date;


}



var parentRec = new GlideRecord('table_a');


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


parentRec.end_date = outputDate;


parentRec.update();


}




})(current, previous);



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



Thanks


Antin


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


I believe that worked, thank you very much for your help with this