Creating Business Rule to add Mentor from Lifecycle event case

aguanci
Tera Expert

Hello,

 

We are attempting to add the opened_by field from the Lifecycle event case table as the Mentor on a Journey. To do this we understand that we need to create a Business Rule on the sn_jny_journey table, but are running into difficulties when configuring the action. None of the options in the drop down are viable to create an action with so we are left to scripting in the action. When searching online or Community we have not been able to find anything. Any help with this problem would be greatly appreciated! 

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi @rocio2 

 

Seems your post was deleted. about a BR on the sn_hr_le_case table to update mentors on sn_jny_journey.

 

The BR I show is defined on the 'sn_jny_journey' table, and runs when updates are made to that record.  As @aguanci screenshot of his/her BR shows.  And it looks in 'sn_hr_core_case' (also works if sn_hr_le_case table is specified as sn_hr_le_case is a child of sn_hr_core_case, which has the fields jny_context and opened_by) for the 'opened_by' value (I tried that based on the xml posted). And that works in my PDI.

 

A BR defined on the 'sn_hr_le_case' table would differ. However your screenshot shows infoMessages from my code. So I'm confused as you say you made an update on the Lifecycle case and not the Journey record.

 

I've tried a BR on that table, Runs "After" for Insert/Update (since it'll update a record in the sn_jny_journey table).  The Script follows

 

 

 

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

	// Add your code here
	var jnyRec = new GlideRecord('sn_jny_journey');
	jnyRec.addQuery('sys_id', current.jny_context.toString());
	jnyRec.query();
	gs.addInfoMessage('BR Found ' + jnyRec.getRowCount() + ' Journey records for ' +current.jny_context.getDisplayValue());
	if (jnyRec.next()) {
		gs.addInfoMessage("Checking if mentor = " + current.opened_by.getDisplayValue() + " for " + current.number);
		var mentorList = jnyRec.mentors.toString();
		var openedBy = current.opened_by.toString();
		// Check for existing values
		if (mentorList.length > 0) {
			// check to see if mentors allready has lifecycle events opended_by
			if (mentorList.indexOf(openedBy) < 0) {
				// Not present, will add
				gs.addInfoMessage("Adding mentor = " + current.opened_by.getDisplayValue() + " to " + current.number);
				jnyRec.mentors += ',' + openedBy;
			}
		}
		else {
			gs.addInfoMessage("Setting mentor = " + current.opened_by.getDisplayValue() + " to " + current.number);
			jnyRec.mentors = openedBy;
		}
		gs.addInfoMessage("Mentors = " + jnyRec.mentors);
		jnyRec.update();
	}
	else
		gs.addInfoMessage("Did not find Journey record for " + current.jny_context);

})(current, previous);

 

 

 

 And it is defined in the 'Human Resources: Lifecycle Events' application scope. After updates to cross scope access for sn_jny_journey. the BR works.

 

Please be clear on what is desired behavior, use specific table names.

View solution in original post

10 REPLIES 10

Bert_c1
Kilo Patron

Seems your business rule is defined on the wrong table. What is the table name of "LifeCycle Events table" in your instance? I don't see that in my PDI after activating the plugins. Go to the "Tables" module and find the table, post the actual table name here.  Better yet, post the XML version of the corresponding sys_db_object record for the table.

The table name for LifeCycle Events is sn_hr_le_case. As far as the sys_db_object, I am not sure this is correct, but is this what you're looking for? 

xml>
<sys_dictionary>
<active>true</active>
<array>false</array>
<array_denormalized>false</array_denormalized>
<attributes>encode_utf8=false,readonly_clickthrough=true</attributes>
<audit>false</audit>
<calculation/>
<choice>0</choice>
<choice_field/>
<choice_table/>
<column_label>Journey</column_label>
<comments/>
<create_roles/>
<default_value/>
<defaultsort/>
<delete_roles/>
<dependent/>
<dependent_on_field/>
<display>false</display>
<dynamic_creation>false</dynamic_creation>
<dynamic_creation_script/>
<dynamic_default_value/>
<dynamic_ref_qual/>
<element>jny_context</element>
<element_reference>false</element_reference>
<foreign_database/>
<formula/>
<function_definition/>
<function_field>false</function_field>
<internal_type display_value="Reference">reference</internal_type>
<mandatory>false</mandatory>
<max_length>32</max_length>
<name>sn_hr_core_case</name>
<next_element/>
<primary>false</primary>
<read_only>true</read_only>
<read_roles/>
<reference display_value="Journey" name="sn_jny_journey">sn_jny_journey</reference>
<reference_cascade_rule/>
<reference_floats>false</reference_floats>
<reference_key/>
<reference_qual/>
<reference_qual_condition table="sn_jny_journey"/>
<reference_type/>
<sizeclass/>
<spell_check>false</spell_check>
<staged>false</staged>
<sys_class_name>sys_dictionary</sys_class_name>
<sys_created_by>AJG72</sys_created_by>
<sys_created_on>2023-09-27 14:04:27</sys_created_on>
<sys_customer_update>false</sys_customer_update>
<sys_id>cb35c02ddb61b1105670caeb13961979</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_name>Journey</sys_name>
<sys_package display_value="Human Resources: Core" source="sn_hr_core">d4ac3fff5b311200a4656ede91f91af2</sys_package>
<sys_policy/>
<sys_replace_on_upgrade>false</sys_replace_on_upgrade>
<sys_scope display_value="Human Resources: Core">d4ac3fff5b311200a4656ede91f91af2</sys_scope>
<sys_update_name>sys_dictionary_sn_hr_core_case_jny_context</sys_update_name>
<sys_updated_by>AJG72</sys_updated_by>
<sys_updated_on>2023-09-27 14:04:27</sys_updated_on>
<table_reference>false</table_reference>
<text_index>false</text_index>
<unique>false</unique>
<use_dependent_field>false</use_dependent_field>
<use_dynamic_default>false</use_dynamic_default>
<use_reference_qualifier>simple</use_reference_qualifier>
<virtual>false</virtual>
<virtual_type>script</virtual_type>
<widget/>
<write_roles/>
<xml_view>false</xml_view>
</sys_dictionary>
</xml>

Bert_c1
Kilo Patron

That is the dictionary record for the 'jny_context' field. But now I see both tables you mention. And if I understand your goal correct, when a record in the 'sn_jny_journey' is created or updated, you want to update the mentors field with the value of 'opened_by' from the 'sn_hr_core_case' that references the specific 'sn_jny_journey' record.  Since 'mentors' field can contain multiple values, logic for the BR defined on the 'sn_jny_journey' table would be:

 

 

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

	// Add your code here
	var lcEvent = new GlideRecord('sn_hr_core_case');
	lcEvent.addQuery('jny_context', current.sys_id.toString());
	lcEvent.query();
	if (lcEvent.next()) {
		gs.addInfoMessage("Checking if mentor = " + lcEvent.opened_by.getDisplayValue() + " for " + current.number);
		var mentorList = current.mentors.toString();
		var openedBy = lcEvent.opened_by.toString();
		// Check for existing values
		if (mentorList.length > 0) {
			// check to see if mentors allready has lifecycle events opended_by
			if (mentorList.indexOf(openedBy) < 0) {
				// Not present, will add
				gs.addInfoMessage("Adding mentor = " + lcEvent.opened_by.getDisplayValue() + " to " + current.number);
				current.mentors += ',' + openedBy;
			}
		}
		else {
			gs.addInfoMessage("Setting mentor = " + lcEvent.opened_by.getDisplayValue() + " to " + current.number);
			current.mentors = openedBy;
		}
	}
	else
		gs.addInfoMessage("Did not find Lifecycle Events record with Journey = " + current.number);

})(current, previous);

 

 

 

But, the sn_hr_le_case and the sn_jny_journey tables are in two different application scopes. You will need to add a 'cross-scope privilege' record:

 

https://docs.servicenow.com/bundle/vancouver-application-development/page/build/applications/referen...

 

to get the above to work. I created two records, 1 for "Journey Designer" to have read access to "Human Resources: Lifecycle Events" and a 2nd for  "Human Resources: Lifecycle Events" to have read access to "Journey Designer".  The BR is defined in the "Journey Designer" application scope.

 

Also, as a Journey can be added to a Lifecycle events record, do you want to update the Journey mentors when a Lifecycle Events records is created or updated? that would take a second business rule.

 

Bert_c1
Kilo Patron

Hi @rocio2 

 

Seems your post was deleted. about a BR on the sn_hr_le_case table to update mentors on sn_jny_journey.

 

The BR I show is defined on the 'sn_jny_journey' table, and runs when updates are made to that record.  As @aguanci screenshot of his/her BR shows.  And it looks in 'sn_hr_core_case' (also works if sn_hr_le_case table is specified as sn_hr_le_case is a child of sn_hr_core_case, which has the fields jny_context and opened_by) for the 'opened_by' value (I tried that based on the xml posted). And that works in my PDI.

 

A BR defined on the 'sn_hr_le_case' table would differ. However your screenshot shows infoMessages from my code. So I'm confused as you say you made an update on the Lifecycle case and not the Journey record.

 

I've tried a BR on that table, Runs "After" for Insert/Update (since it'll update a record in the sn_jny_journey table).  The Script follows

 

 

 

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

	// Add your code here
	var jnyRec = new GlideRecord('sn_jny_journey');
	jnyRec.addQuery('sys_id', current.jny_context.toString());
	jnyRec.query();
	gs.addInfoMessage('BR Found ' + jnyRec.getRowCount() + ' Journey records for ' +current.jny_context.getDisplayValue());
	if (jnyRec.next()) {
		gs.addInfoMessage("Checking if mentor = " + current.opened_by.getDisplayValue() + " for " + current.number);
		var mentorList = jnyRec.mentors.toString();
		var openedBy = current.opened_by.toString();
		// Check for existing values
		if (mentorList.length > 0) {
			// check to see if mentors allready has lifecycle events opended_by
			if (mentorList.indexOf(openedBy) < 0) {
				// Not present, will add
				gs.addInfoMessage("Adding mentor = " + current.opened_by.getDisplayValue() + " to " + current.number);
				jnyRec.mentors += ',' + openedBy;
			}
		}
		else {
			gs.addInfoMessage("Setting mentor = " + current.opened_by.getDisplayValue() + " to " + current.number);
			jnyRec.mentors = openedBy;
		}
		gs.addInfoMessage("Mentors = " + jnyRec.mentors);
		jnyRec.update();
	}
	else
		gs.addInfoMessage("Did not find Journey record for " + current.jny_context);

})(current, previous);

 

 

 

 And it is defined in the 'Human Resources: Lifecycle Events' application scope. After updates to cross scope access for sn_jny_journey. the BR works.

 

Please be clear on what is desired behavior, use specific table names.

rocio2
Tera Contributor

Thank you so much!!!!! 

This works perfectly!!!