glide evaluator

RiteshI
Tera Contributor
getDynamicValue: function(valueString, table, record, displayValue, doNA){
        var returnVal = valueString;
        var regexp = /\{([^}]+)\}/g;
        var toReplace;
        while ((toReplace = regexp.exec(valueString))){
            var replaceVal;
            var field = toReplace[1].toString();
            toReplace = toReplace[0].toString();
            var rec = new GlideRecord(table);
            if (rec.get(record)) {
                if (displayValue == true)
                    replaceVal = eval ('rec.' + field + '.getDisplayValue()');
                else
                    replaceVal = eval('rec.' + field);
               
                //If replaceVal is nil and it's not a barcode field, return 'N/A'
                if (replaceVal == '' && doNA == true)
                    replaceVal = 'N/A';
               
                returnVal = returnVal.replace(toReplace, replaceVal);
                if (this.debug == 1)
                    gs.log('BarGen: Returning dynamic value = ' + replaceVal);
            }
        }
        return returnVal;
    }---------------->want to replace eval with glide evaluator ?
 
11 REPLIES 11

Chaitanya ILCR
Kilo Patron

Hi @RiteshI ,

You don't need GlideScopedEvaluator for this

try this

 

getDynamicValue: function(valueString, table, record, displayValue, doNA) {
    var returnVal = valueString;
    var regexp = /\{([^}]+)\}/g;
    var toReplace;
    while ((toReplace = regexp.exec(valueString))) {
        var replaceVal;
        var field = toReplace[1].toString();
        toReplace = toReplace[0].toString();
        var rec = new GlideRecord(table);
        if (rec.get(record)) {
            if (displayValue == true)
                replaceVal = rec[field].getDisplayValue(); //eval ('rec.' + field + '.getDisplayValue()');
            else
                replaceVal = rec[field]; //eval('rec.' + field);

            //If replaceVal is nil and it's not a barcode field, return 'N/A'
            if (replaceVal == '' && doNA == true)
                replaceVal = 'N/A';

            returnVal = returnVal.replace(toReplace, replaceVal);
            if (this.debug == 1)
                gs.log('BarGen: Returning dynamic value = ' + replaceVal);
        }
    }
    return returnVal;
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

but I know I dont need glide evaluator for this but for other script I need so I wanted to know how to implement glideEvaluator

 

 

HI @RiteshI ,

 

GlideEvaluatorAPI is usually meant for running script inside the script fields

in your scenario there is no scope for this API.

 

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...

 

//setting up a record that contains the script to be executed.
//Example from docs
var now_GR = new GlideRecord('u_global_table'); 
now_GR.u_short_description = 'Calculate Addition';  
now_GR.u_test_script = "result = x + y";
evaluator.evaluateScript(now_GR, "script") 
now_GR.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.putVariable('result', null);
evaluator.evaluateScript(now_GR, "script")     //now_GR is the GlideRecord and "script" is the script field bakend name
now_GR.insert();

 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

RiteshI
Tera Contributor

hey @Chaitanya ILCR can I make use of glideEvaluator in UI script