
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2017 05:07 PM
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);
Solved! Go to Solution.
- Labels:
-
Scoped App Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 03:20 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-29-2017 06:14 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 07:35 AM
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?
- (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?
- children.addQuery('table_a', current.table_a); // Should I be querying for a specific field that is same in both tables?
- children.addQuery('sys_id', '!=', current.sys_id); // Shouldn't I be looking for the sys_id that matches the parent
- 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'); // This is the parent table correct?
- if(parentRec.get(current.table_a)){ // What should I be looking for here?
- parentRec.expected_completion_date = outputDate; // expected_completion_date is the date field within the parent table I'm trying to set
- parentRec.update();
- }
- })(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 09:06 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2017 01:44 PM
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
Parent Table: Due Date Field