Hi Flow action script execution from custom table

Not applicable

Hi All,

I have a requirement for the script execution in the flow action. We have created a Custom table with a field type as "Script". Now, If any script is written in that field. I need to run that script in the flow action. How can I achieve this? I tried by giving the "input.variable" in the Script flow. But no luck and I tried to call directly the field inside the flow script but also it didn't worked. But when I have written the script in the custom flow action script and defined only 1 input. It worked. Can anyone help me on this? PFA images for ref.

RipunjaiGurava_0-1709033728159.png

RipunjaiGurava_1-1709033922722.png

Thanks,

Ripunjai

vermaamit16
Kilo Patron

Hi @Community Alums 

 

You can make use of GlideScopedEvaluator() API. Refer below post and give this a try in your Script Step :

https://www.servicenow.com/community/developer-forum/how-can-i-run-a-script-stored-in-a-quot-script-...

https://www.servicenow.com/community/developer-forum/execute-script-in-custom-script-field/m-p/15553...

 

Thanks & Regards

Amit Verma

Thanks and Regards
Amit Verma

Sandeep Rajput
Tera Patron

@Community Alums You can use GlideScopeEvaluator to evaluate the script stored in a field on a table.

 

Here is an example.

 

// Setting up a record that contains the script to be executed.
var now_GR = new GlideRecord('u_global_table'); 
now_GR.u_short_description = 'Calculate Addition';  
now_GR.u_test_script = "result = x + y"; 
now_GR.insert(); 
 
var evaluator = new GlideScopedEvaluator();
evaluator.putVariable('x', 100);
evaluator.putVariable('y', 200);
evaluator.evaluateScript(now_GR, 'u_test_script', null);
gs.info(evaluator.getVariable('result'));

 

Please refer the URL to know more about GlideScopeEvaluator https://developer.servicenow.com/dev.do#!/reference/api/vancouver/server/no-namespace/c_GlideEvaluat... 

Not applicable

Hi @vermaamit16 . @Sandeep Rajput ,

Thanks for helping. I updated the script the same way as per the doc. But it shows an error stating the variable is not defined. Can you check, did I updated the Flow designer action and script the correct way?

Script added in the custom table

 

var grFlowActionFlowAction = new GlideRecord('u_service_id');
grFlowActionFlowAction.addQuery('sys_id','0080be48dba48120fbc899fbd3961964');
grFlowActionFlowAction.query();
if(grFlowActionFlowAction.next()){
grFlowActionFlowAction.u_service_value_1= 'scriptupdate_23/02_1';
grFlowActionFlowAction.update();
gs.log('Hello world_23-02');
}
Script added in Flow action script
(function execute(inputs, outputs) {
// ... code ...
var evaluator = new GlideScopedEvaluator();
evaluator.evaluateScript(grFlowActionFlowAction, 'u_service_value_1', null);
gs.info(evaluator.getVariable('result'));

})(inputs, outputs);
 
Error
RipunjaiGurava_1-1709100204042.png

 

RipunjaiGurava_0-1709100181787.png

 Thanks, 

Ripunjai

Hi @Community Alums 

 

Update the flow action script as below and retry :

 

 

var grFlowActionFlowAction= new GlideRecord('u_service_id');
grFlowActionFlowAction.addQuery('sys_id','0080be48dba48120fbc899fbd3961964');
grFlowActionFlowAction.query();
if(grFlowActionFlowAction.next()){
  var evaluator = new GlideScopedEvaluator();
  gs.info(evaluator.evaluateScript(grFlowActionFlowAction, 'u_script', null))); // Please replace u_script with your script field name in the table
}

 

 

Thanks & Regards

Amit Verma

Thanks and Regards
Amit Verma