Calling an Autoscript from an Autoscript

It is possible to have an autoscript call another autoscript. The reason you might do this would include reducing the number of launch points or autoscripts that trigger off a record. (Instead of 5 you could have one that then called the other autoscripts as needed). This also allows you to help guarantee the order in which autoscripts are triggered.

There are a couple of ways to do this. First is to call the script directly:

service.invokeScript("YOUR SCRIPT NAME")

This will execute the script. That script will have access to the system variables (e.g. mbo) that your current script has. When the script is done, it will continue executing your code in the calling script. This example is great if you just want to execute the script but don’t need to pass it any specific values. However, it would be nice if you could pass in values to the script. Turns out this can be done. If the child script has variables, those can be passed in using a hashmap.

For example, let’s say we have a script that multiples two values (“IN” variables X and Y) and stores the results in and “OUT” variable Z. We would call that script this way:

// from java.util import HashMap
x = 5
y = 3
vars = HashMap()
vars.put("X",x)
vars.put("Y",y)
service.invokeScript("Multiply",vars)
z = vars.get("Z")

The above code calls a script called “Multiply” and passes in a 5 and a 3. Then retrieves the value Z from the script after it has executed. Using this method allows you to create functions that you can call within any script.

Now it turns out there is another way to call another script and that is by calling it as an ACTION. First you would create a script with an action launch point. Then you would call it this way:

actionSet = MXServer.getMXServer().getMboSet("ACTION", mbo.getUserInfo())
 
actionSet.setWhere("action = 'action_name'")
actionSet.reset()
actionSet.getMbo(0).executeAction(mbo)

This code would call an action called ‘action_name’. It may appear that this approach is less desirable as 1) the only variable you can pass in a mbo object and 2) it takes more lines of code. However, the fact that you pass in a mbo object is where this method shines. There is nothing saying that the mbo object has to be the current mbo object. You could be processing an Asset object but call a script that takes a work order mbo.

So why would you want to call a script from another script? After all, you could just create a script with a launch point and have it run that way. And although that is true, it is often that we need to have an update on one object trigger an update on another object. If we just create two scripts with two launch points, then the second script has to have conditions to see if it is time to execute (as we would only want it to run if the first script executes (by checking for some data element set by the first script). This means that the script is going to executed every time and the secondary object is changed, even if it makes no changes. Doing this too much can be a performance issue. However, if you instead had the first script directly call the second script (so the second script has no launching point), then it is executed only when needed.

Both of these methods have their place and uses and are pieces of code to keep in your coding toolbox.