GlideScopedEvaluator without a GlideRecord?

Cary5
Mega Expert

So I very seldom use eval() (i.e. evil) but tend to use it when I want to loop through an enumerated group of fields and perform the same function. Example: There are 5 date fields on a ritm that carrying the syntax start_dateX where X is a number 1 through 5. Since i'm in a scoped application, it was recommended via error that I use GlideScopedEvaluator() vs. eval(). However, it seems I can only use this API if I'm evaluating a script field from a GlideRecord. What if I have a variable that contains what I want to evaluate?

I'm open to other suggestions if there is a better way to approach. In this case, I have 5 type, start, and end date fields to capture (15 variables). The count of the fields could change in the future. Count is actually stored in a system property so additional line items can be added in the future (using the same naming convention for the fields).

// Grab approved leave requests for user

  var ritm = new GlideRecord('sc_req_item');

  ritm.addQuery('cat_item', sys_id);

  ritm.addQuery('request.requested_for', user);

  ritm.addQuery('approval', 'approved');

  ritm.query();

  while (ritm.next()){

        var leave = [];

        var count = 5;

        for (x=1; x < count; x++){

                  var evaluator = new GlideScopedEvaluator();

                  var start_date_field = "ritm.variables.start_date" + x; // My "field" that I want to evaluate

                  var start_date_val = evaluator.evaluateScript(ritm, start_date_field);

                  leave.push({

                        start_date: start_date_val

                  });

        }

}

Message was edited by: Cary Trusty -- Fixed code and applied syntax

3 REPLIES 3

christianmehrin
Tera Contributor

How about initializing a dummy glideRecord for:



function foo_scoped() {


  var dummy = new GlideRecord('sys_script');


  dummy.initialize();



  dummy.script = "gs.print(value); var answer = 1";



  var evaluator = new GlideScopedEvaluator();


  evaluator.putVariable("value", "Test");


  evaluator.putVariable('answer', null);



  evaluator.evaluateScript(dummy, "script");



  gs.print(evaluator.getVariable("answer"));


}


Hi Christian,



Good suggestion. As far as I can tell, using a GlideRecord seems to be the only way to execute an eval within a scoped application. Unfortunately, what I've discovered is that you actually have to save the GlideRecord for the evaluator to use it. Additionally you have to have to use a table with a script field that you can Create/Read/Delete to from within your application scope, sys_script table is not cross scope editable by default. So here is a working version of the code you sent.


1. I created a custom dummy table in my application scope, which includes a script field.




foo_scoped();


function foo_scoped() {


      var dummy = new GlideRecord('x_8899_integration_dummy_script');


      dummy.initialize();




      dummy.script = "gs.info(value); var answer = 1";


      var sid = dummy.insert();




      if (dummy.get(sid)){


      var evaluator = new GlideScopedEvaluator();


      evaluator.putVariable("value", "Test");


      evaluator.putVariable('answer', null);




      evaluator.evaluateScript(dummy, "script");




      gs.info(evaluator.getVariable("answer"));


      dummy.deleteRecord();


      }


}


Cary5
Mega Expert

It seems silly now but I ended up figuring out that I can avoid the eval by using this notation since 'variables' is an array of all the values collected from the form.



// Grab approved leave requests for user


  var ritm = new GlideRecord('sc_req_item');


  ritm.addQuery('cat_item', sys_id);


  ritm.addQuery('request.requested_for', user);


  ritm.addQuery('approval', 'approved');


  ritm.query();



  while (ritm.next()){


        var leave = [];


        var count = 5;


        for (x=1; x < count; x++){


              gs.print(ritm.variables["start_date" + x]);


        }


}