
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 06:03 AM
We have a script that is running in our Request workflow that evaluates whether a request should be sent to a Director level employee that I would like to duplicate in a scoped application that I am building but the current script doesn't work because it is using eval(), which is not allowed in scoped apps, I am being told to use GlideScopedEvaluator() instead. A partner wrote this code during our implementation and I cannot get it to work using GlideScopedEvaluator.
Any help with this is appreciated. Here is the script:
// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.
//
answer = [];
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', current.opened_by);
gr.query();
if (gr.next()) {
var mgr = 'gr.manager';
var nextmgr = 'manager';
var i = 0;
while (i <= 6) {
//gs.print(eval(mgr+'.getDisplayValue();'));
//gs.print(eval(mgr+'.u_job_code.getDisplayValue();'));
var jc = parseInt(eval(mgr+'.u_job_code.getDisplayValue().substring(0,1);'));
//gs.print(jc);
//Director or above, then stop
if (jc <= 3) {
var appr = eval(mgr+';');
//gs.print(eval(mgr+'.getDisplayValue();'));
//gs.print(eval(mgr+'.u_job_code.getDisplayValue();'));
answer.push(appr);
break;
}
//at the top - stop.
if (jc == 1) {
break;
}
mgr += '.' + nextmgr;
i++;
}
}
Solved! Go to Solution.
- Labels:
-
Scoped App Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 07:48 AM
Here's something you can try. I tested a little bit, but obviously not with your custom u_job_code column or data.
answer = [];
answer.push(getDirector(current.getValue('opened_by')));
function getDirector(userID) {
var answer = '';
var user = new GlideRecord('sys_user');
if (user.get(userID)) {
var mgr = user.getValue('manager');
if (mgr != '') {
var mgrCode = user.manager.u_job_code.getDisplayValue().substring(0,1);
if (parseInt(mgrCode) <= 3) {
answer = mgr;
}
else {
answer = getDirector(mgr);
}
}
}
return answer;
}
I think both this and Michael's code below should get you where you need to go. In my example here, if a user did not have a manager before you got to the director it would return blank and skip the approval, so that may be something you want to account for.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 07:03 AM
Just to the director. We have Supervisors and Managers that are below Directors whose Job Codes begin with 5 and 4. Director's Job Codes begin with a 3. So the script was using the sys_user.manager field to find the manager of the person that entered the request, check their Job Code, and if it was not low enough (begin with a 3) move on to the next manager and so on.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 07:48 AM
Here's something you can try. I tested a little bit, but obviously not with your custom u_job_code column or data.
answer = [];
answer.push(getDirector(current.getValue('opened_by')));
function getDirector(userID) {
var answer = '';
var user = new GlideRecord('sys_user');
if (user.get(userID)) {
var mgr = user.getValue('manager');
if (mgr != '') {
var mgrCode = user.manager.u_job_code.getDisplayValue().substring(0,1);
if (parseInt(mgrCode) <= 3) {
answer = mgr;
}
else {
answer = getDirector(mgr);
}
}
}
return answer;
}
I think both this and Michael's code below should get you where you need to go. In my example here, if a user did not have a manager before you got to the director it would return blank and skip the approval, so that may be something you want to account for.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 07:51 AM
Also, I forgot to add some sort of counter so you don't get stuck in a manager loop.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 11:47 AM
That worked! And it seems to be working without a counter. It is working with only the IF statement. I have tested several scenarios and even when it walks up 3 levels, it grabs the correct approver with no issue.
Thanks Brad!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-14-2017 07:11 AM
Something like the following should work. As Brad says you don't need to use eval() and its "evil" anyway.
Your old script is using a getDisplayValue() on the u_job_code so assume its a reference. You need to figure out what column in the job code table is used as the display value and put that column name into the script where indicated below. I took the liberty to change "gr" to a real descriptive name as I have gotten burned many times using "gr" in scripts. Good practice from a debugging perspective is to name your variables something that makes sense to the coder and future "readers" of the code.
var userRec = new GlideRecord('sys_user');
userRec.addQuery('sys_id', current.opened_by);
userRec.query();
if (userRec.next()) {
var i = 0;
var currentManager = "manager";
while (i <= 6) {
var managerJobCode = parseInt(userRec.getElement(currentManager + ".u_job_code.WHATEVER-THE-DISPLAY-VALUE-COLUMN-IS").substring(0,1));
if (managerJobCode <= 3) {
var managerID = userRec.getElement(currentManager);
answer.push(managerID);
break;
}
if (managerJobCode == 1) {
break;
}
currentManager = currentManager + ".manager";
i++;
}
}