<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Update Child records help Scripting in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815823#M472749</link>
    <description>&lt;P&gt;I would like to know if anyone can help me with figuring out how to update secondary records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following script that will update the parent record (all records are parent unless the parent field is filled in)&amp;nbsp; I need to find all secondary records and set the u_triage_sla_time on them.&amp;nbsp; But not sure how to accomplish that.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Need to do with this script:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; 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.&lt;/P&gt;</description>
    <pubDate>Fri, 25 Sep 2020 13:41:18 GMT</pubDate>
    <dc:creator>Steve42</dc:creator>
    <dc:date>2020-09-25T13:41:18Z</dc:date>
    <item>
      <title>Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815823#M472749</link>
      <description>&lt;P&gt;I would like to know if anyone can help me with figuring out how to update secondary records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have the following script that will update the parent record (all records are parent unless the parent field is filled in)&amp;nbsp; I need to find all secondary records and set the u_triage_sla_time on them.&amp;nbsp; But not sure how to accomplish that.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(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);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Need to do with this script:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; 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.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 13:41:18 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815823#M472749</guid>
      <dc:creator>Steve42</dc:creator>
      <dc:date>2020-09-25T13:41:18Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815824#M472750</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;(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);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Sep 2020 13:52:15 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815824#M472750</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-25T13:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815825#M472751</link>
      <description>&lt;P&gt;It's running as an after rule since the time does not get set until someone is assigned to the alert&lt;/P&gt;
&lt;P&gt;Ok so this should be a maintenance rule then?&amp;nbsp; I'm new to the event management module.&amp;nbsp; 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.&lt;/P&gt;
&lt;P&gt;So should I just make this BR into a maintenance rule then?&amp;nbsp; How are maintenance rules run with the em_alerts?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 14:30:43 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815825#M472751</guid>
      <dc:creator>Steve42</dc:creator>
      <dc:date>2020-09-25T14:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815826#M472752</link>
      <description>&lt;P&gt;Hi Steve,&lt;/P&gt;
&lt;P&gt;It's an alert management rule sorry, not a maintenance rule...too many things beginning with M for a Friday.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;1. Create a new Alert Management Rule with a clear name&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47232iF0262873782E932D/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Set the filter to run when the alert record changes to meet the condition of Acknowledged = True &amp;amp; Role is Primary&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47235iC65471904DAA09A7/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;4. Open Flow Designer and navigate to 'Subflows' and find the Alert Management Template record. Open it&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47231iAB90D977BE2FE4D5/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;5. Using the action menu copy the flow and give it a name&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47239i4F153FF957FB938F/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;6. Our first action is to update the alert record. We use the 'Update Record' action and the input-&amp;gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47233i7B2509969EA2D3ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;7. We then do a 'Look Up Alert Records' to find all the child records.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47230i50BDC56493A8B145/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47236i0CDFDC647982D51A/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;9. We then add a 'For Each Item in' to update all the secondary records.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47234iDAF248011C4499EC/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;10. And again, same as the first update record. Save &amp;amp; Publish your creation&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47237i859881213686E28F/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="find_real_file.png"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/47238iB9E5D8D66240B751/image-size/large?v=v2&amp;amp;px=999" role="button" title="find_real_file.png" alt="find_real_file.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And that should be that. If you're not feeling like doing the above, I captured what I did in an update below &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 15:35:51 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815826#M472752</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-25T15:35:51Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815827#M472753</link>
      <description>&lt;P&gt;Dude You Rock!!!!!&amp;nbsp; That's awesome worked like a champ.&amp;nbsp; Now I need to see if I can do the same type of thing for sn_si_incidents.&amp;nbsp; 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.&amp;nbsp; Kudo's!!!&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 16:29:27 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815827#M472753</guid>
      <dc:creator>Steve42</dc:creator>
      <dc:date>2020-09-25T16:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815828#M472754</link>
      <description>&lt;P&gt;Glad it worked! Once you get your head around alert management and flow designer you start to see the power of the platform &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 16:39:26 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815828#M472754</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-25T16:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815829#M472755</link>
      <description>&lt;P&gt;Do you know if you can use the same thing for sn_si_incidents?&lt;/P&gt;
&lt;P&gt;I have the need to do basically the exact same thing&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:37:55 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815829#M472755</guid>
      <dc:creator>Steve42</dc:creator>
      <dc:date>2020-09-28T15:37:55Z</dc:date>
    </item>
    <item>
      <title>Re: Update Child records help Scripting</title>
      <link>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815830#M472756</link>
      <description>&lt;P&gt;Hey Steve,&lt;/P&gt;
&lt;P&gt;I've not done much with SIR or any of the SecOps modules. You won't be able to use alert management rules as these are specific to ITOM Event Management but the concept of flow designer could be used AFAIK.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 16:04:21 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/update-child-records-help-scripting/m-p/1815830#M472756</guid>
      <dc:creator>Kieran Anson</dc:creator>
      <dc:date>2020-09-28T16:04:21Z</dc:date>
    </item>
  </channel>
</rss>

