Daniel Borkowi1
Mega Sage

Hi community,

 

within my team we had a discussion about scripting within Flow Designer. We realized that we created tons of Script Actions for one time scripts. This caused our flow action list to grow immeasurably and makes us doubt that we will be able to manage this in the future. Especially when script actions are no longer needed because the associated flows are no longer used.

 

We have come up with an approach that I would like to put up for discussion here.
Here's the idea: we store such scripts in Script Includes and call them via a generic script action. What do you think of this and if you don't think it's a bad idea, perhaps you have some suggestions for improving the approach?

 

Now, step by step, what we have come up with:

 

Create a Script Action "Run Script"

First define 3 Input Parameters:

  1. Name of Script Include where the function is placed
  2. Name of Function within the Script include which should be called
  3. Array of Strings as Input values for the function

DanielBorkowi1_0-1712759930910.png

 

Use Script Step to call the Script Include:

  • Ensure that input parameters are available within script by mapping the input variablesDanielBorkowi1_1-1712760059974.png
  • Use inputs variables to build a function call script and utilize the dynamic capabilities to instantiate a Script Include class and call a function (Edit: thanks to @rsander without utilizing evil eval anymore)image.png

 

 

(function execute(inputs, outputs) {
    var SI_Class = JSUtil.getGlobal()[inputs.script_include];      
    var result = new SI_Class()[inputs.function_name](inputs.input);   
    outputs.return_object = result;
})(inputs, outputs);

 

 

  • Don't forget to define the output variables for the script step - in our case a String Array called return_object

DanielBorkowi1_3-1712760210890.png

 

Map the Script Step Output to the Action Output

 

DanielBorkowi1_4-1712760264286.png

 

Create a Script Include to encapsulate your scripts

As our script action allows to define the Script Include name you can use for each usage a new Script Include or you use a central Script Include and adds new functions for each use case. It's your choice.

Here is an example how this SI could look like:

DanielBorkowi1_0-1712761739899.png

Here is the code of this example Script Include:

 

 

 

var ScriptActionTestSI = Class.create();
ScriptActionTestSI.prototype = {
    initialize: function() {
    },
	test_function : function(input){
		var a = input[0];
		var b = input[1];
		result = [];
		result[0] = a+b;
		result[1] ="DBOR";
		return result;
	},
    type: 'ScriptActionTestSI'
};

 

 

 

You can create any Script Include and define your own functions, names doesn't matter, as you define them as input in the Run Script action. We have only two restrictions regarding the function signature:

  1. As the input parameter needs to be generic, we decided to use a Array of Strings. If you need complex objects there, you can use JSON.stringify()  and JSON.parse() to transfer these.
  2. Also the output should be generic, that's why we also have chosen a string array.

But as mentioned, it's not really a restriction because these string could also contain complex objects in JSON format.

 

Usage of Script Action within flow

As an example the flow simple calls the new action and logs the result afterwards:

DanielBorkowi1_1-1712762173685.png

DanielBorkowi1_2-1712762230013.png

The result of this flow:

DanielBorkowi1_0-1712904382159.png

 

Conclusion

To summarise, my intention is not to recommend this approach as best practice, but rather to hear your opinion on it. From our point of view, this saves a lot of single use actions, but adds another level of complexity, in which the good old script include now has to be used in addition to the flow and action editor.

So fire away, what's your opinion dear community.

 

7 Comments