Hi Flow action script execution from custom table

Community Alums
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

1 ACCEPTED SOLUTION

Hi @Community Alums 

 

Let me give you a step by step explanation. Suppose I have a custom table 'u_testing' with a Script type column 'u_inputscript' which has a simple script to return sum of two numbers. Refer below screenshots :

 

AmitVerma_0-1709121702155.png

 

AmitVerma_1-1709121721057.png

 

AmitVerma_2-1709121745178.png

 

AmitVerma_3-1709121766991.png

 

Now if the following script from Scripts-Background, I will get the sum of both the numbers i.e. 6

 

AmitVerma_4-1709121839343.png

 

AmitVerma_5-1709121854184.png

 

We are already passing the GlideRecord reference while calling the evaluateScript method. It expects the GlideRecord reference and the name of the column in which your script resides. Hope it clears your query.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

8 REPLIES 8

Amit Verma
Kilo Patron
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-quot-type-field/m-p/1745993

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

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Sandeep Rajput
Tera Patron
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_GlideEvaluatorScopedAPI#r_ScopedGlideEvaluatorGlideScopedEvaluator?navFilter=glideScope 

Community Alums
Not applicable

Hi @Amit Verma . @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


Please mark this response as correct and helpful if it assisted you with your question.