- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2020 02:17 PM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 10:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 10:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 10:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 12:12 PM
Michael,
That is perfect. I greatly appreciate your help. Thank you!
Shane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2020 12:14 PM
You are very welcome! Glad I could help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2020 11:22 AM
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);