Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to use GlideScriptEvaluator to remove eval() method from the existing scripts?

nirwan_ritik
Tera Contributor
 
1st Script:
       
var response1 = r.execute();
        var responseBody1 = response1.getBody();
        var httpStatus1 = response1.getStatusCode();
        var ans = (JSON) ? JSON.parse(responseBody1) : eval(responseBody1);
        for (var i = 0; i < ans.length; i++) {
            if (ans[i].usr_username == userID) {
                str[j] = ans[i].app_pro_title.replace(/_|\-/, " ") + '-' + ans[i].app_number;

                j++;
            }
         }
 
2nd Script:
                var par = [];
                for (var i=levelValue; i < gr.getValue("hierarchy_level"); i++) {
                    par.push(relationalColumn);
                }
                eval("var p = gr."+par.join("."));

how to remove these eval() executions and replace them with GlideScriptEvaluator. 
 Kindly help me regarding this 🙂
2 REPLIES 2

Anand Kumar P
Giga Patron

Hi @nirwan_ritik ,

Creating an object for GlideScriptEvaluator you can use as below mentioned

var response1 = r.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
var scriptEvaluator = new GlideScriptEvaluator();
var ans = (JSON) ? JSON.parse(responseBody1) : scriptEvaluator.evaluateString(responseBody1);
for (var i = 0; i < ans.length; i++) {
    if (ans[i].usr_username == userID) {
        str[j] = ans[i].app_pro_title.replace(/_|\-/, " ") + '-' + ans[i].app_number;
        j++;
    }
}
var par = [];
for (var i = levelValue; i < gr.getValue("hierarchy_level"); i++) {
    par.push(relationalColumn);
}
var scriptEvaluator = new GlideScriptEvaluator();
var script = "var p = gr." + par.join(".");
scriptEvaluator.evaluateString(script);

https://www.servicenow.com/community/developer-articles/introduction-to-glidescopedevaluator-api-eva... 

Please mark it as solution proposed and helpful if its serves your purpose.

Thanks,

Anand



nirwan_ritik
Tera Contributor

Hi Anand, 

Thanks for the quick reply. I will execute this in my lower instances. I'll mark the solution helpful post execution. Will revert to you if I see any issues. 
Thanks again 🙂