The CreatorCon Call for Content is officially open! Get started here.

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!

1 ACCEPTED SOLUTION

Sure give the updated script a try, conditions stay the same.  What it does is:

  • Loops through the kb_managers and compiles two lists now: 1 for users with certification role and one of names of users without
  • If there are invalid users, the error message will appear (which I updated slightly so feel free to edit)
  • If there are valid users with proper roles, it will update the kb_managers value to just those users, thus removing the invalid users
  • If there are no valid users, the update will abort and won't save.

 

(function executeRule(current, previous /*null when async*/) {
	
	var errorMessage = "The following user(s) were removed because rhe ServiceNow Access form needs completed: ";
	var validUsers = [];
	var invalidUserNames = [];
	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 invalidUserNames variable.
		// If user has valid roles, add them to the validUsers so the record can be updated
		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]);
			invalidUserNames.push(userRec.getDisplayValue());
		} else {
			validUsers.push(managerList[i]);
		}
	}
	
	// Present error message to user letting them know of users without proper roles.
	if (invalidUserNames.length > 0) {
		errorMessage += invalidUserNames.toString();
		gs.addErrorMessage(errorMessage);
	}
	
	// If users with valid roles have been found, set kb_managers to that list of users.  If none found abort save.
	if (validUsers.length > 0) {
		current.kb_managers = validUsers.toString();
	} else {
		current.setAbortAction(true);
	}

})(current, previous);

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

View solution in original post

13 REPLIES 13

Michael,

     Your business rule is fantastic.  Would there be any way of not aborting, but only adding the users with an existing role?  I can update the message as appropriate.

Thank you very much,

Shane

Sure give the updated script a try, conditions stay the same.  What it does is:

  • Loops through the kb_managers and compiles two lists now: 1 for users with certification role and one of names of users without
  • If there are invalid users, the error message will appear (which I updated slightly so feel free to edit)
  • If there are valid users with proper roles, it will update the kb_managers value to just those users, thus removing the invalid users
  • If there are no valid users, the update will abort and won't save.

 

(function executeRule(current, previous /*null when async*/) {
	
	var errorMessage = "The following user(s) were removed because rhe ServiceNow Access form needs completed: ";
	var validUsers = [];
	var invalidUserNames = [];
	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 invalidUserNames variable.
		// If user has valid roles, add them to the validUsers so the record can be updated
		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]);
			invalidUserNames.push(userRec.getDisplayValue());
		} else {
			validUsers.push(managerList[i]);
		}
	}
	
	// Present error message to user letting them know of users without proper roles.
	if (invalidUserNames.length > 0) {
		errorMessage += invalidUserNames.toString();
		gs.addErrorMessage(errorMessage);
	}
	
	// If users with valid roles have been found, set kb_managers to that list of users.  If none found abort save.
	if (validUsers.length > 0) {
		current.kb_managers = validUsers.toString();
	} else {
		current.setAbortAction(true);
	}

})(current, previous);

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

Michael,

      That is perfect.  I greatly appreciate your help.  Thank you!

Shane

You are very welcome!  Glad I could help.

Michael,

      I am finally able to get back to this.  The script works perfectly when adding users as knowledge managers.  However, when a KB manager tries to remove anyone without adding any persons, the system gives the error as in the attached screenshot.

     I found an out-of-box business rule named "Update knowledge_manager roles" and I've included its information below.  Can you please help with the best way that I should move forward?

     Thank you very much for your help.

 

OOB Business Rule:  "Update knowledge_manager roles"

Run After, Insert, Update

When to run: Owner IS NOT EMPTY or Managers IS NOT EMPTY

var knowledgeSecurityManager = new KnowledgeSecurityManager();
// Create an array of user sys_ids to verify manager roles
var currentManagers = current.kb_managers.split(",");
if (currentManagers != undefined)
	currentManagers.push(current.owner);
else currentManagers = [current.owner];

var previousManagers = previous.kb_managers.split(",");
if (previousManagers != undefined) 
	previousManagers.push(previous.owner) 
else previousManagers = [previous.owner];

knowledgeSecurityManager.updateKnowledgeManagers(currentManagers, previousManagers);