How to user GlideScopedEvaluator();

Kyle Wiley
Mega Expert

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++;

}

}

1 ACCEPTED SOLUTION

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.


View solution in original post

9 REPLIES 9

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.


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.


Also, I forgot to add some sort of counter so you don't get stuck in a manager loop.


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!


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

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++;


      }


}