Business rule script looking at roles users have

shanedavis
Tera Expert

I have a design that I need scripting help please.  On the Knowledge Base [kb_knowledge_base], when a Manger [kb_managers] adds one or more managers, I need a business rule that will determine if the user(s) being added only have the 'certification' role and, if so, stops submission and gives a message that the ServiceNow Access form needs completed for the user(s) listing their names in the message.

Thank you for any help that you can provide!

Michael Ritchie
ServiceNow Employee

In my opinion a more user friendly approach would be to set an advanced reference qualifier on the Managers list to ONLY allow the selection of users WITH the "certification" role.  Allowing someone to choose something and then tell them later it is invalid isn't a good user experience.

I agree with you Michael.  However, because we have been with ServiceNow so many years, our licensing structure is different.  Therefore, I am basically having to weed out the 'certification' role to verify if the user has any roles other than 'certification'.

Gotcha.  The following should work:

  • Create a new business rule on Knowledge Base (kb_knowledge_base)
  • Set the Order to 1 or something lower than 100 where OOB business rules execute and add roles to the users
  • Set condition to Managers changes and Managers is not empty:

find_real_file.png

Then on the advanced tab the following script.  I made an assumption you are utilizing the OOB "Certification" role so the SysID should work.  But feel free to modify the errorMessage variable to your liking.

(function executeRule(current, previous /*null when async*/) {
	
	var errorMessage = "The ServiceNow Access form needs completed for the following user(s): ";
	var userNames = [];
	var managerList = current.kb_managers.toString().split(",");
	for (var i = 0; i < managerList.length; i++) {
		// Search the User Role table for roles assigned to each manager.  Filtering out the "Certification" role.
		// If no additional roles are found, add the user's name to the userNames variable and abort update.
		var userRole = new GlideRecord("sys_user_has_role");
		userRole.addQuery("user", managerList[i]);
		userRole.addQuery("role", "!=", "ba4509c60a00070400cc0f3a60a0d30a"); //Certification
		userRole.query();
		if (!userRole.hasNext()) {
			var userRec = new GlideRecord("sys_user");
			userRec.get(managerList[i]);
			userNames.push(userRec.getDisplayValue());
		}
	}
	
	if (userNames.length > 0) {
		errorMessage += userNames.toString();
		gs.addErrorMessage(errorMessage);
		current.setAbortAction(true);
	}

})(current, previous);

find_real_file.png

 

Please mark this post or any as helpful or the correct answer to your question if applicable so others viewing may benefit.

Michael,

     I am going to be testing the solution this morning and will mark your answer correct based on my testing.  Thank you very much for the detail and screenshots!!  I appreciate the effort that you put in to help me.  I'll let you know how things go today.  

 

Thank you,

Shane