Exclude some users while writing business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 03:37 AM
Hello All,
I have a requirement where I need to exclude some users(John ty,Umily gour) while writing business rule. If user is from the list even though he has title svp he shouldn't get assigned as manager
(function executeRule(current, previous /*null when async*/ ) {
var appOwner = current.owned_by.toString();
//First level of manager
var smng1 = usr.manager;
var susrMng1 = new GlideRecord('sys_user');
susrMng1.addQuery('name', smng1);
susrMng1.get(mng1);
susrMng1.query();
gs.addInfoMessage('++++usrMng1.title: ' + usrMng1.title);
while (susrMng1.next()) {
if ((susrMng1.title == 'SVP') || (susrMng1.title == 'Senior Vice President')) {
current.u_exec_direct = smng1.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++mng1: ' + smng1);
} else {
//second level of manager
var smng2 = susrMng1.manager;
var susrMng2 = new GlideRecord('sys_user');
susrMng2.addQuery('name', smng2);
susrMng2.get(smng2);
susrMng2.query();
while (susrMng2.next()) {
if ((susrMng2.title == 'SVP') || (susrMng2.title == 'Senior Vice President')) {
current.u_exec_direct = smng2.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user3: ' + susrMng2);
} else {
//Third level of manager
var smng3 = susrMng2.manager;
var susrMng3 = new GlideRecord('sys_user');
susrMng3.addQuery('name', smng3);
susrMng3.get(smng3);
susrMng3.query();
while (susrMng3.next()) {
if ((susrMng3.title == 'SVP') || (susrMng3.title == 'Senior Vice President')) {
current.u_exec_direct = smng3.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user4: ' + susrMng3);
} else {
//Fourth level of Manager
var smng4 = susrMng3.manager;
var susrMng4 = new GlideRecord('sys_user');
susrMng4.addQuery('name', smng4);
susrMng4.get(smng3);
susrMng4.query();
while (susrMng4.next()) {
if ((susrMng4.title == 'SVP') || (susrMng4.title == 'Senior Vice President')) {
current.u_exec_direct = smng4.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user5: ' + susrMng4);
} else {
//Fifth level of manager
var smng5 = susrMng4.manager;
var susrMng5 = new GlideRecord('sys_user');
susrMng5.addQuery('name', smng5);
susrMng5.get(smng3);
susrMng5.query();
while (susrMng5.next()) {
if ((susrMng5.title == 'SVP') || (susrMng5.title == 'Senior Vice President')) {
current.u_exec_direct = smng5.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user6: ' + susrMng5);
}
}
}
}
}
}
}
}
}
}
}
current.update();
}
}
)(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 05:48 AM
First let's address some coding errors. When you set a variable to sys_user.manager it will contain a sys_id (GUID). So your addQuery('name', smgr<x>) lines will not return any values. Also, you have a typo in which you are referring to smng3 instead of smng4 So the excerpt:
var smng4 = susrMng3.manager;
var susrMng4 = new GlideRecord('sys_user');
susrMng4.addQuery('name', smng4);
susrMng4.get(smng3);
susrMng4.query();
while (susrMng4.next()) {
if ((susrMng4.title == 'SVP') || (susrMng4.title == 'Senior Vice President')) {
current.u_exec_direct = smng4.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user5: ' + susrMng4);
should be replace with the following:
var smng4 = susrMng3.manager;
var susrMng4 = new GlideRecord('sys_user');
//susrMng4.addQuery('name', smng4); //This line will cause the query to return no values
susrMng4.get(smng4); //Fixed typo, should be smng4, not smng3
//susrMng4.query(); //Don't call query() after calling get() as it is redundant
if (susrMng4.next()) { //Changed WHILE to IF since there is only one value returned from the query
if ((susrMng4.title == 'SVP') || (susrMng4.title == 'Senior Vice President')) {
current.u_exec_direct = smng4.toString();
current.u_exec_owner = current.owned_by;
current.u_engineer_contact=current.u_exec_direct.manager.toString();
gs.addInfoMessage('++++user5: ' + susrMng4);
This is just a review of one excerpt but you should review the entire code and correct any similar errors.
Next, rather than put the user names or IDs in the code, I would suggest introducing a custom attribute in your sys_user table to override this functionality so that users can be excluded from this function if desired. Then you can just add that condition to your existing condition.
The opinions expressed here are the opinions of the author, and are not endorsed by ServiceNow or any other employer, company, or entity.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 06:32 AM
Thanks for quick reply and also correcting my code. Regarding your solution for the question to create a custom field to query it its not possible they want me to capture all those user names in system property and they want me to call them in code to exclude but when I try that its not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 06:35 AM
Thanks for quick reply and correcting my code. Regarding your solution to question its not possible to create a custom field in user table and add condition, They want me to create a system property which contains all the names and call them in the code so that that users will be excluded but when I try that its not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2023 11:57 AM
From looking at your code I don't see anywhere that you are getting a system property of a list of names to exclude, so I can only guess here but I am guessing that you are probably querying the name incorrectly. If your list of names is a list of User IDs then you will need to query the user_name field of the user; if it is a list of full names I would suggest not doing this because of the risk of human error and name changes; otherwise, if it is a list of sys_id values, do the needful. Beyond that I'd need to look at the code to tell you why it isn't working.
The opinions expressed here are the opinions of the author, and are not endorsed by ServiceNow or any other employer, company, or entity.