Replacing Eval with GlideScriptEvaluator

Vetcha Prasanna
Tera Contributor

Hello ,

 

I have a function in client callable script include as below:

      var ritmVar = '';
        var item = new GlideRecord("sc_req_item");
        item.get(sysid);
        //If RITM found
        if (item) {
            ritmVar += 'Requested For: ' + item.request.requested_for.getDisplayValue() + '\n';
            ritmVar += 'Stage: ' + item.stage.getDisplayValue() + '\n';
            ritmVar += 'Opened: ' + item.opened_at + '\n';
            ritmVar += 'Request: ' + item.short_description + '\n\n';

            var mtom = new GlideRecord('sc_item_option_mtom');
            mtom.addQuery('request_item', item.sys_id);
            mtom.orderBy('sc_item_option.order');
            mtom.query();
            while (mtom.next()) {
                var questionIsActive = mtom.sc_item_option.item_option_new.active;
                var answer = mtom.sc_item_option.value;
                if (questionIsActive && !answer.nil()) {
                    ritmVar += mtom.sc_item_option.item_option_new.question_text + ": " + eval('item.variable_pool.' + mtom.sc_item_option.item_option_new.name + '.getDisplayValue()') + "\n";
                }
            }
        }
        return ritmVar;
    },

In above , i need to replace eval function with GlideScript Evaluator. Please someone can help me on above script?


4 REPLIES 4

Mayur2109
Kilo Sage
Kilo Sage

Hi @Vetcha Prasanna ,

Please replace eval with GlideEvaluator.evaluateString.

 

 

 var ritmVar = '';
        var item = new GlideRecord("sc_req_item");
        item.get(sysid);
        //If RITM found
        if (item) {
            ritmVar += 'Requested For: ' + item.request.requested_for.getDisplayValue() + '\n';
            ritmVar += 'Stage: ' + item.stage.getDisplayValue() + '\n';
            ritmVar += 'Opened: ' + item.opened_at + '\n';
            ritmVar += 'Request: ' + item.short_description + '\n\n';
			gs.info(item.variable_pool);

            var mtom = new GlideRecord('sc_item_option_mtom');
            mtom.addQuery('request_item', item.sys_id);
            mtom.orderBy('sc_item_option.order');
            mtom.query();
            while (mtom.next()) {
                var questionIsActive = mtom.sc_item_option.item_option_new.active;
                var answer = mtom.sc_item_option.value;
                if (questionIsActive && !answer.nil()) {
                    ritmVar += mtom.sc_item_option.item_option_new.question_text + ": " + GlideEvaluator.evaluateString('item.variable_pool.' + mtom.sc_item_option.item_option_new.name + '.getDisplayValue()')+ "\n";
                }
            }
        }

 

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

ServiceNow Rising Star 2024

Thanks Mayur. But unfortunately its not working. I am getting the error like item is not defined.

I am testing as below
calling the above function(script include) from background script by passing the RITM sys_id and it gives the null values.
Could you please help me on this.

Rajesh Chopade1
Mega Sage

Hi @Vetcha Prasanna

you can utilize following script:

var ritmVar = '';
var item = new GlideRecord("sc_req_item");
item.get(sysid);

// If RITM found
if (item) {
    ritmVar += 'Requested For: ' + item.request.requested_for.getDisplayValue() + '\n';
    ritmVar += 'Stage: ' + item.stage.getDisplayValue() + '\n';
    ritmVar += 'Opened: ' + item.opened_at + '\n';
    ritmVar += 'Request: ' + item.short_description + '\n\n';

    var mtom = new GlideRecord('sc_item_option_mtom');
    mtom.addQuery('request_item', item.sys_id);
    mtom.orderBy('sc_item_option.order');
    mtom.query();

    var evaluator = new GlideScopedEvaluator();
    while (mtom.next()) {
        var questionIsActive = mtom.sc_item_option.item_option_new.active;
        var variableName = mtom.sc_item_option.item_option_new.name;
        var answer = mtom.sc_item_option.value;

        if (questionIsActive && !answer.nil()) {
            // Build the script to evaluate
            var script = 'item.variable_pool.' + variableName + '.getDisplayValue();';
            var displayValue = evaluator.evaluateScript(item, script);
            ritmVar += mtom.sc_item_option.item_option_new.question_text + ": " + displayValue + "\n";
        }
    }
}
return ritmVar;

I hope my answer helps you to resolve your issue, if yes please mark my answer as helpful & correct

thank you

rajesh

Hello Rajesh,

Thanks for the reply.

But unfortunately its not working.
I am testing as below
calling the above function(script include) from background script by passing the RITM sys_id and it gives the null values.
Could you please help me on this.