Script to update Child Incident fields to match Parent

StewartF
Tera Expert

We have a requirement in the company to sync certain fields down from a Parent Incident to each of its Child Incidents. This are the Business Service, Assignment Group and Assigned To.

 

Little wrinkle to that is you can only select an Assigned To that is in the Assignment Group chosen.

 

So I've had a Business Rule in for a while that runs After Update of the Parent Incident, and it will check the Child to make sure things match.

 

However, it's become clear recently that this isn't working correctly, and in fact every single time a worknote or comment are added to the Parent, or another INC is linked to the Parent, then every single Child Incident is getting their Assigned To wiped and re-added - even on Closed tickets, which is then triggering closed emails and surveys again.

 

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

    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent_incident', current.sys_id);
    childIncidents.query();

    if (childIncidents.assignment_group != current.assignment_group) {
        while (childIncidents.next()) {
            childIncidents.assigned_to = '';
            childIncidents.assignment_group = current.assignment_group;
            childIncidents.update();
            childIncidents.assigned_to = current.assigned_to;
            childIncidents.update();
        }
    }
})(current, previous);
 
This is the script in the BR - I'm not a great scripter (questionable if I'm even any good) so there's likely something I'm missing here. Any ideas/thoughts?
 
I had to put in about wiping the Assigned To first due to what I mentioned initially about the fact the Assigned To has to be part of the Assignment Group, and if you try to change the AG without wiping, it errors.
1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

HI,

 

You cannot use childIncidents.assignment_group before .next() as it will not have any value to compare

Try this

 

 

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

    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent_incident', current.sys_id);
    childIncidents.query();


        while (childIncidents.next()) {
    if (childIncidents.assignment_group != current.assignment_group) { //you cannot use this before .next()
            childIncidents.assigned_to = '';
            childIncidents.assignment_group = current.assignment_group;
            childIncidents.update();
            childIncidents.assigned_to = current.assigned_to;
            childIncidents.update();
        }
    }
})(current, previous);

 

-Anurag

View solution in original post

6 REPLIES 6

Vrushali  Kolte
Mega Sage

Hello @StewartF ,

 

Can you try below script -

 

(function executeRule(current, previous /*null when async*/) {
    // Query for child incidents
    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent_incident', current.sys_id);
    childIncidents.query();

    while (childIncidents.next()) {
        // Check if the child incident's assignment group is different from the current incident's assignment group
        if (childIncidents.assignment_group != current.assignment_group) {
            // Clear the assigned_to field
            childIncidents.assigned_to = '';
            // Update the assignment group
            childIncidents.assignment_group = current.assignment_group;
            childIncidents.update();
        }

        // Update the assigned_to field to match the current incident's assigned_to
        if (childIncidents.assigned_to != current.assigned_to) {
            childIncidents.assigned_to = current.assigned_to;
            childIncidents.update();
        }
    }
})(current, previous);

 

If my answer solves your issue, please mark it as Accepted and Helpful!

Anurag Tripathi
Mega Patron
Mega Patron

HI,

 

You cannot use childIncidents.assignment_group before .next() as it will not have any value to compare

Try this

 

 

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

    var childIncidents = new GlideRecord('incident');
    childIncidents.addQuery('parent_incident', current.sys_id);
    childIncidents.query();


        while (childIncidents.next()) {
    if (childIncidents.assignment_group != current.assignment_group) { //you cannot use this before .next()
            childIncidents.assigned_to = '';
            childIncidents.assignment_group = current.assignment_group;
            childIncidents.update();
            childIncidents.assigned_to = current.assigned_to;
            childIncidents.update();
        }
    }
})(current, previous);

 

-Anurag

See what I meant when I said it's questionable if I'm even a good scripter? Haha thank you @Anurag Tripathi that works exactly how it's supposed to now!

Cheers mate. 

-Anurag