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

Mark Manders
Mega Patron

In your case it would be better to move to the flow designer. It's easy to use and shows exactly what you are doing. For people that don't are magicians with scripts, that often the best way to get what they need.

Trigger it on 'assignment group change, assigned to change or business service change' and do a look up to all incidents where the parent = the record triggered. If nothing found, end flow. Else: for each found incident update the fields to that of the parent.

 

For your BR: trigger it the change of any of the three fields you need synced and then use this script:

 

var childIncident = new GlideRecord('incident');
    childIncident.addQuery('parent_incident',current.getUniqueValue()); // validate that this is indeed the correct field
    childIncident.addQuery('active',true);
    childIncident.next();
    while childIncident.next(){
        if(childIncident.assigned_to != current.assigned_to){
            childIncident.assigned_to = current.assigned_to;
        }
        if(childIncident.assignment_group != current.assignment_group){
            childIncident.assignment_group = current.assignment_group;
        }
        if(childIncident.business_service != current.business_service){
            childIncident.business_service = current.business_service;
        }
        childIncident.update();
    }

 




Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Robbie
Kilo Patron
Kilo Patron

Hi @StewartF,

 

So a couple of observations here.

Firstly, add some filter conditions so the business rule only runs if one of the 3 fields you mentioned (Business Service, Assignment Group and Assigned To) change, thus make sure it doesn't trigger after every update.

See first screen shot below.

 

Secondly, I would also add a clause to ensure you're only checking with 'active' incidents (Non closed tickets).

Question - do you also want to consider if the Child Incidents are active as well?

 

You'll need to add a line similar to:

childIncidents.addQuery('active', true); // Checking for active child incidents only (Not all including closed)

 

Screen shot:

Screenshot 2024-07-24 at 11.57.23.png

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie