- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-03-2022 01:50 PM - edited 11-04-2022 01:56 AM
Hi,
I've been trying to get GlideScopedEvaluator() to work, by injecting script from a custom script field into a script include. However, nothing works, except eval(), which should not be used for obvious reasons.
What I'm trying to do:
1. inbound email action starts processing incoming new email
2. inbound email action script calls script include "XMLhandler":
(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var emailBody = email.body_text;
if (gs.getProperty('some_sys_property_name').indexOf(email.from) > -1) {
var handleXml = new XMLhandler(emailBody); // call script include "XMLhandler"
current.contact_type = 'some_value';
}
if (current.contact_type == 'some_value') {
current.insert();
}
})(current, event, email, logger, classifier);
3. script include glides to custom mapping table, which has a script field called u_custom_script and it should execute this script, as if it would be part of the script include, which eval() does nicely as per my testing, but GlideScopedEvaluator() fails to do.
My script in custom table field u_custom_script:
if (someCondition) {
current.assignment_group = '[sys_id]'; //assignment group 1
} else {
current.assignment_group = '[sys_id]'; //assignment group 2
}
As you can see, while creating a ticket, I want the current.assignment_group value to be defined in the custom script field. I'm not interested in passing any variables back to script include and settings these there.
Here is part of the script include "XMLhandler", that I am having issues with (:
var parent = "something";
var customTable = new GlideRecord('u_custom_table');
customTable.addQuery('u_parent', parent);
customTable.query();
while (customTable.next()) {
// Assign values based on form mappings
current.assignment_group = customTable.u_assignment_group; //assign default value
//..had many more, but minified it for the purpose of this example
// Run custom script
if(customTable.u_run_custom_script){ //if u_run_custom_script is true
/* //Following works fine, but uses eval(), which is bad
var script = customTable.u_custom_script;
var result = Function("return " + script)();
eval(script); */
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('current', current);
evaluator.evaluateScript(customTable, 'u_custom_script', null);
//evaluator.getVariable("current", current);
}
}
Any tips and tricks, how I can get evaluator to work, so that User A adds custom script into custom table script field, then the script is executed as part of the script include, and in turn inbound email action script.
The rest of the solution otherwise works fine, however, not the assignment group custom script part.
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 08:05 AM - edited 11-04-2022 08:07 AM
I solved it..
In script include, I had the following:
if (customTable.u_run_custom_script) { //if u_run_custom_script is true
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('current', current); //send current to custom script
evaluator.putVariable('varForSomeConditions', varForSomeConditions); //send other to current script
evaluator.evaluateScript(customTable, 'u_custom_script', null); //run script form custom script field
}
And in custom script field I wrapped it inside function:
(function () {
if (someCondition) {
current.assignment_group = '[sys_id]'; //assignment group 1
} else if (someOtherCondition) {
current.assignment_group = '[sys_id]'; //assignment group 2
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2022 08:05 AM - edited 11-04-2022 08:07 AM
I solved it..
In script include, I had the following:
if (customTable.u_run_custom_script) { //if u_run_custom_script is true
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('current', current); //send current to custom script
evaluator.putVariable('varForSomeConditions', varForSomeConditions); //send other to current script
evaluator.evaluateScript(customTable, 'u_custom_script', null); //run script form custom script field
}
And in custom script field I wrapped it inside function:
(function () {
if (someCondition) {
current.assignment_group = '[sys_id]'; //assignment group 1
} else if (someOtherCondition) {
current.assignment_group = '[sys_id]'; //assignment group 2
}
})();