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

DUGGI
Giga Guru

Hi Kyle,



this script may give you idea


4.1 evaluateScript(GlideRecord grObj, String scriptField, Object variables)

Evaluates a script from a GlideRecord field.


Parameters:


  • grObj - (GlideRecord) a "queried" GlideRecord containing the script expression.
  • scriptField - (String) [optional] the name of the field containing the script expression.
  • variables - (Object) [optional] a map of variables with "variable name" - "variable value" pairs to be used in the evaluation. These variables are available to the script in the GlideRecord during this method execution.

Returns:


Object - result of the script execution.

Example:


// For this example, we created a table: "x_app_table" with two columns: "short_description", "test_script" // "test_script" will store the script to be evaluated by GlideScopedEvaluator. gr = new GlideRecord('x_app_table'); gr.short_description = 'Testing GlideScopedevaluator'; gr.test_script = "gs.getUser().getName() + ' says ' + greeting; "; gr.insert();   // setup variables to be used by the script var vars = {'greeting' : 'hello'};   //Evaluate the script from the field var evaluator = new GlideScopedEvaluator(); gr = new GlideRecord('x_app_table'); gr.addQuery('short_description','Testing GlideScopedevaluator'); gr.query(); if (gr.next()) { gs.info(evaluator.evaluateScript(gr, 'test_script', vars)); }

DUGGI
Giga Guru

// For this example, we created a table: "x_app_table" with two columns: "short_description", "test_script"


// "test_script" will store the script to be evaluated by GlideScopedEvaluator.


gr = new GlideRecord('x_app_table');


  1. gr.short_description = 'Testing GlideScopedevaluator';  
  2. gr.test_script = "gs.getUser().getName() + ' says ' + greeting; ";
  3. gr.insert();


// setup variables to be used by the script


var vars = {'greeting' : 'hello'};



//Evaluate the script from the field


var evaluator = new GlideScopedEvaluator();


gr = new GlideRecord('x_app_table');


  1. gr.addQuery('short_description','Testing GlideScopedevaluator');
  2. gr.query();

if (gr.next()) {


      gs.info(evaluator.evaluateScript(gr, 'test_script', vars));


I saw that article in the WIKI page but I don't understand how it works.   I have tried to change the example to fit the existing code we have and it isn't working.   I need help specifically with out script.


What's the requirement here? Is the script supposed to send the approval to every manager up the org tree until it gets to a director or just just to a director? If it's one of those you should be able to rewrite this as a recursive function rather than doing eval.