
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2017 07:09 AM
I have the below business rule that updates a due date field from the child record. Right now, each time the child end date is updated the rule iterates through all of the child records and updates the parent due date to the end date of the child record that is furthers out because a record can have multiple child records. I need the rule to not make an update until all of the child end dates are populated. So if the end date is updated for one child record, but the other end dates on the other child records are not populated, the script should stop and not make an update. If all of the end dates are populated then the script should continue and update the parent record. Can anyone assist with this set up?
(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);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2017 07:36 AM
I think I got the jist of what you are doing so I reorganized a few thing but this should do it. So I think some logic this doesnt check is if the parentRec.expected_completion_date has a value existing and there is a null end_date on a child(maybe new child with no end date was created) it doesnt set the parentRec.expected_completion_date back to a empty value. that is something you can add in easily if you want.
(function executeRule(current, previous /*null when async*/) {
var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');
if(parentRec.get(current.parent)){
if (parentRec.submission_type == 'Change Request') {
var outputDate;
var aggNullDates = new GlideAggregate('x_hemas_connectus2_task');
aggNullDates.addEncodedQuery('active=true^end_date=NULL^parent='+parentRec.sys_id);
aggNullDates.addAggregate('COUNT');
aggNullDates.query();
var countNull = 0;
if(aggNullDates.next()){
countNull = aggNullDates.getAggregate('COUNT');
}
if(countNull == 0){
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;
}
parentRec.expected_completion_date = outputDate;
parentRec.update();
}
}
}
}})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2017 07:17 AM
Hi Edwin,
Give this a try. You'll need to scan all records with the while instead of just the first record with the "if". I commented out the orderByDesc() operator to get all records. Only that way can you check for any empty entries and jump out of the loop. Note, untested code ahead.
(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();
var outputDate = new GlideDateTime("1970-01-01 12:00:00");;
while (children.next()) {
// If any are empty...
if (children.end_date.nil()) {
return; // stop the BR script
}
if (children.end_date > outputDate)
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2017 07:36 AM
I think I got the jist of what you are doing so I reorganized a few thing but this should do it. So I think some logic this doesnt check is if the parentRec.expected_completion_date has a value existing and there is a null end_date on a child(maybe new child with no end date was created) it doesnt set the parentRec.expected_completion_date back to a empty value. that is something you can add in easily if you want.
(function executeRule(current, previous /*null when async*/) {
var parentRec = new GlideRecord('x_hemas_connectus2_x_hemas_connectus_connectus');
if(parentRec.get(current.parent)){
if (parentRec.submission_type == 'Change Request') {
var outputDate;
var aggNullDates = new GlideAggregate('x_hemas_connectus2_task');
aggNullDates.addEncodedQuery('active=true^end_date=NULL^parent='+parentRec.sys_id);
aggNullDates.addAggregate('COUNT');
aggNullDates.query();
var countNull = 0;
if(aggNullDates.next()){
countNull = aggNullDates.getAggregate('COUNT');
}
if(countNull == 0){
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;
}
parentRec.expected_completion_date = outputDate;
parentRec.update();
}
}
}
}})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-03-2017 07:49 AM
Good thinking Nathan. I was also thinking of doing GlideAggregate to count for null values. Your method is a tad bit quicker if there are lots of children.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-04-2017 07:37 PM
Thank you Nathan, it worked perfectly