Update Child records help Scripting

Steve42
Tera Expert

I would like to know if anyone can help me with figuring out how to update secondary records. 

I have the following script that will update the parent record (all records are parent unless the parent field is filled in)  I need to find all secondary records and set the u_triage_sla_time on them.  But not sure how to accomplish that.

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

	var parentID = current.sys_id;
	var tableName = 'em_alert';
	//var assignedTo = current.u_assigned_to;
	
	var gdt = new GlideDateTime(new Date());
		
	var emAlert = new GlideRecord(tableName);
	emAlert.addQuery('sys_id',parentID);
	emAlert.query();
    while (emAlert.next()){
		//gs.info('@@@ UpdateTriageTime completed at ' + gdt);
		emAlert.setValue('u_triage_sla_time',gdt);
		emAlert.update();
	}
	
	

})(current, previous);

Need to do with this script:

1.  Find any other record in the em_alert table that has the parent field the same as the current sys_id (all child records) I can do the update part just need to know how to find all child (secondary) alerts.

1 ACCEPTED SOLUTION

Hi Steve,

It's an alert management rule sorry, not a maintenance rule...too many things beginning with M for a Friday.

Maintenance rules can run when a record changes to a value or is created with a set of values. They can then run subflows (which this will) automated actions to automatically resolve issues or allow manual operations via the "Quick Action" of an operator.

1. Create a new Alert Management Rule with a clear name

find_real_file.png

 

2. Set the filter to run when the alert record changes to meet the condition of Acknowledged = True & Role is Primary

find_real_file.png

3. Save the record, it'll show an alert saying it's moved to in-active as it doesn't have an action. We'll make that next

 

4. Open Flow Designer and navigate to 'Subflows' and find the Alert Management Template record. Open it

find_real_file.png

 

5. Using the action menu copy the flow and give it a name

find_real_file.png

 

6. Our first action is to update the alert record. We use the 'Update Record' action and the input->alertGR data pill for the record. Sadly Flow doesn't have the ability to set the time to the current time as of yet, so we need to use the inline script feature.

 

find_real_file.png

 

7. We then do a 'Look Up Alert Records' to find all the child records.

find_real_file.png

 

 

8. We then add a if statement to check we have some records. If we don't, and for whatever reason the lookup returns 0, the flow will error.

find_real_file.png

 

9. We then add a 'For Each Item in' to update all the secondary records.

find_real_file.png

10. And again, same as the first update record. Save & Publish your creation

find_real_file.png

11. Go back to the alert management rule and update the 'actions' section with the subflow created. I've set it to run automatically once. Also need to set the rule to active.

find_real_file.png

 

And that should be that. If you're not feeling like doing the above, I captured what I did in an update below 🙂 

 

View solution in original post

7 REPLIES 7

Kieran Anson
Kilo Patron

Hi,

When is this rule running (the one you've added) on insert? It's best practice to be using maintenance rules on the alert table rather than business rules due to the data turn over that occurs.

Regarding your direct requirements, the following BR will update the u_triage_sla_time of the current (i.e parent) record along with its child records.

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

	// Add your code here
	var gdt = new GlideDateTime();
	
	current.setValue('u_triage_sla_time', gdt);
	
	var secondaryGR = new GlideRecord(current.sys_class_name);
	secondaryGR.addQuery('parent',current.getUniqueValue());
	secondaryGR.query();
	while(secondaryGR.next()){
		secondaryGR.setValue('u_triage_sla_time',gdt);
		secondaryGR.update();
	}

})(current, previous);

It's running as an after rule since the time does not get set until someone is assigned to the alert

Ok so this should be a maintenance rule then?  I'm new to the event management module.  The business rule runs when the correlation _rule is 0 or 1 and the Assigned to field is not null and the state is not closed.

So should I just make this BR into a maintenance rule then?  How are maintenance rules run with the em_alerts?

 

Hi Steve,

It's an alert management rule sorry, not a maintenance rule...too many things beginning with M for a Friday.

Maintenance rules can run when a record changes to a value or is created with a set of values. They can then run subflows (which this will) automated actions to automatically resolve issues or allow manual operations via the "Quick Action" of an operator.

1. Create a new Alert Management Rule with a clear name

find_real_file.png

 

2. Set the filter to run when the alert record changes to meet the condition of Acknowledged = True & Role is Primary

find_real_file.png

3. Save the record, it'll show an alert saying it's moved to in-active as it doesn't have an action. We'll make that next

 

4. Open Flow Designer and navigate to 'Subflows' and find the Alert Management Template record. Open it

find_real_file.png

 

5. Using the action menu copy the flow and give it a name

find_real_file.png

 

6. Our first action is to update the alert record. We use the 'Update Record' action and the input->alertGR data pill for the record. Sadly Flow doesn't have the ability to set the time to the current time as of yet, so we need to use the inline script feature.

 

find_real_file.png

 

7. We then do a 'Look Up Alert Records' to find all the child records.

find_real_file.png

 

 

8. We then add a if statement to check we have some records. If we don't, and for whatever reason the lookup returns 0, the flow will error.

find_real_file.png

 

9. We then add a 'For Each Item in' to update all the secondary records.

find_real_file.png

10. And again, same as the first update record. Save & Publish your creation

find_real_file.png

11. Go back to the alert management rule and update the 'actions' section with the subflow created. I've set it to run automatically once. Also need to set the rule to active.

find_real_file.png

 

And that should be that. If you're not feeling like doing the above, I captured what I did in an update below 🙂 

 

Dude You Rock!!!!!  That's awesome worked like a champ.  Now I need to see if I can do the same type of thing for sn_si_incidents.  I have the same thing to do on those but with this as a template I might be able to accomplish the exact same results.  Kudo's!!!