Replacing Eval with GlideScriptEvaluator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-10-2024 11:57 PM
Hello ,
I have a function in client callable script include as below:
In above , i need to replace eval function with GlideScript Evaluator. Please someone can help me on above script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 12:29 AM - edited 09-11-2024 12:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 09:57 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 12:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 09:56 AM
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.