- 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
01-31-2020 02:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2020 02:32 PM
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'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2020 02:57 PM
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:
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);
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 06:47 AM
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