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

Hi agaunci,

 

Create a new business rule on the 'sn_jny_journey' table that for "When to run" has When "After" and for "Insert" and "Update", click Advanced, and for the script use:

 

 

 

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

	// Add your code here
	var journeyRec = new GlideRecord('[name_of_journey_table]');
	journeyRec.initialize();
	journeyRec.mentor = current.opened_by;
	// set additional journeyRec fields as desired
	// ...
	var newRec = journeyRec.insert();
	if (newRec)
		gs.addInfoMessage("Created new Journey record with mentor = ' + current.opened_by.getDisplayValue());
	else
		gs.addInfoMessage("Failed to create new Journey record with mentor = ' + current.opened_by.getDisplayValue());

})(current, previous);

 

 

 Once you've tested the BR, remove or comment out the gs.addInfoMessage() lines if you like. You should be able to find similar OOB business rules in your instance, as examples you can follow. Search with name, starts with, 'Create'.

 

BTW, I tried activating Journey and Lifecycle related plugins in my instance and didn't get any table with 'journey' in their name, nor any field with 'mentor' as the name. So I can't test. I suspect you've left out needed context to receive help here.

Hi Bert_c1,

 

Thank you for your quick response! After installing the Journey and Journey Accelerator plugins, we found that the table is called sn_jny_journey. This script will not work as we are trying to pull the opened_by user from the LifeCycle Events table. We need to find a way to call on the LifeCycle Events case table to populate the Mentor for the Journey. 

Bert_c1
Kilo Patron

Hi Aguanci,

 

Do you mean the 'sn_hr_le_case' being the "LifeCycle Events" table? A BR defined on that table should work, it has the 'opened_by' field . I have the sn_jny_journal now and it has a field named 'mentors'. So try:

 

	journeyRec.mentors = current.opened_by.toString();

in the script I posted.

No, we need to build the script of the Journey table (sn_jny_journey) but somehow pull in data (opened_by) from the LifeCycle Events table. Here is what the business rule looks like so far:

 

MentorBR.png