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

Hi Edwin,



I am not sure about the Script, but I dont think it would solve your user case. You use case is much simpler than that. The below script would solve your use case. I have updated the same in your other thread also.



(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);




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



Thanks


Antin


Does this script run on the child table or parent table? I'm assuming table b below is the child table, I have questions since I wasn't able to get it working for me? Can you review my questions below within the script?



  1. (function executeRule(current, previous /*null when async*/) {  
  2.  
  3.  
  4. var outputDate = current.end_date;  
  5. var children = new GlideRecord('x_hemas_connectus2_task'); // This is the child table correct?  
  6. children.addQuery('table_a', current.table_a); // Should I be querying for a specific field that is same in both tables?    
  7. children.addQuery('sys_id', '!=', current.sys_id); // Shouldn't I be looking for the sys_id that matches the parent  
  8. children.query();  
  9. while(children.next()){  
  10. if(outputDate < children.end_date)  
  11. outputDate = children.end_date;  
  12. }  
  13.  
  14. var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus'); // This is the parent table correct?  
  15. if(parentRec.get(current.table_a)){ // What should I be looking for here?
  16. parentRec.expected_completion_date = outputDate; // expected_completion_date is the date field within the parent table I'm trying to set  
  17. parentRec.update();  
  18. }  
  19.  
  20.  
  21. })(current, previous);  

antin_s
ServiceNow Employee
ServiceNow Employee

Hi Edwin,



The Script/Business Rule runs on the Child table. In my script,



table_a   -> Parent Table


table_b -> Child Table


table_b.table_a -> Parent's table Reference on Child Table




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




var outputDate = current.end_date;


var children = new GlideRecord('x_hemas_connectus2_task'); // This is the child table correct? - <Antin> Yes.


children.addQuery('table_a', current.table_a); // Should I be querying for a specific field that is same in both tables? - <Antin> Here 'table_a' is the name of the field in Child table that refers the Parent table


children.addQuery('sys_id', '!=', current.sys_id); // Shouldn't I be looking for the sys_id that matches the parent - <Antin> - You dont need to change this line because it looks for the sys_id on the child record


children.query();


while(children.next()){


if(outputDate < children.end_date)   // <Antin> end_date is the date field within the child table


outputDate = children.end_date;


}



var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus'); // This is the parent table correct? - <Antin> - Yes


if(parentRec.get(current.table_a)){ // What should I be looking for here? -   <Antin> Samed as Line # 6. 'table_a' is the name of the field in Child table that refers the Parent table


parentRec.expected_completion_date = outputDate; // expected_completion_date is the date field within the parent table I'm trying to set - <Antin> - Yes


parentRec.update();


}




})(current, previous);



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



Thanks


Antin


Thanks - I was able to get the code working, but it is still not exactly doing what I am expecting. Notice in the form screen shot below the furthest planned date out is 10-06-2017 yet the script set the parent date to 09-08-2017.




Current Script



(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;  


}  


 


var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');  


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


parentRec.expected_completion_date   = outputDate;  


parentRec.update();  


}  


 


 


})(current, previous);  


   




Child Table: Child records


find_real_file.png


Parent Table: Due Date Field


find_real_file.png