Script activity - using scripts in the workflow
Step-by-step guide
What is the Script Activity?
- How do you use the Script Activity without input variables?
- How do you use the Script Activity with input variables?
What is the Script Activity?
When executing a workflow, we may want to run a chosen script using different values from the workflow. The script activity enables us to define output variables based on scripts where we use input variables available in the workflow. It is also possble to run a script without any input variables. Both of these options would result in output variable values which can then be used during the workflow execution. The script activity can be added anywhere in a workflow, but if you are using input variables the script activity cannot be placed before the element where you pick up the input variable value.
The script activity can be used in innumerable ways, depeding on what you may want to transform or calculate. A Script Activity can be created/edited in the Workflow Builder, and you do not have to create a separate items for this in the Organizer.
How do you use a Script Activity without Input variables?
In the following example we will use the Script Activity to run a script pick up the current day and time. In this example we will not use any input variables and the output from the script activity will be the date and time. These two output variables will be used to populate fields in a PDF template.
To build this workflow, we will use the following pre-exiting elements:
- An uploaded PDF Template with two input fields ("date" and "time").
The created workflow will include:
- A Start point
- A Script activity
- A PDF template activity
- A Receipt activity
- An End point
Only the settings directly connected to the Script Activity will be included in this example. If you need more information about how to set up the other workflow elements, please refer to the documentation.
Start by creating a workflow and add a Start point, a Script Activity, a PDF Template Activity, a Receipt activity and an End point to your workflow.
Then select the Script Activity in the canvas. For this workflow we have chosen to block the progress of the workflow if our script execution fails.
You may enter the script directly in the script activity properties, but if you wish to enter a longer script it can be beneficial to open the code mirror popup.
In the popup, switch to code and enter the preferred groovy script.
The script we are using in this example is the following:
date = Util.currentTimeAsString("dd.MM.yyyy", Util.getLocale(null), Util.getTimeZone("CET")) time = Util.currentTimeAsString("HH:mm:ss", Util.getLocale(null), Util.getTimeZone("CET"))
After entering the script, press Ok to save the script and close the code mirror popup.
The next step is to define the output variables used in the script. Press Output Variables to open the output variables popup.
Add a variable and name it date. Our date should have the data type string and hence we will leave the data type with the default data type String.
Add the second output variable used in the script, time. The time variable will should have the data type string and hence we will leave the data type with the default data type String.
Our output variables have now been defined and we can move on to using the variables to populate our PDF template.
Select the PDF Template Activity in the canvas.
After Browsing to add your PDF Template to the PDF Template activity in the workflow, press Map PDF fields.
Select the first variable from the PDF template "date" and Browse to find the correct Value From Workflow.
Expand the Script Acitivty and select the output variable you wish to use as the value for the selected PDF template variable.
Then repeat the previous steps to connect the variable in the PDF Template with the second script activity output variable.
When both PDF variables are connected to the output variables you can see them in the list of variables and values.
To complete the workflow configure the settings for the remaining workflow activities. Remember to Save the Draft, create a new version of your workflow and start the workflow from the Organizer to set it in production.
Example workflow requires Compose version 5.17.1 or higher: Script Activity without input variables
How do you use a Script activity with Input variables?
In the following example we will use it to run a script to get the age and gender from the entry point authentication. In this example we will use the ssn from BankID authentication as the input variable and the output from the script activity will be the age and the gender of the authenticated person. In this example we will use these to prepopulate form answers.
To build this workflow, we will use the following pre-exiting elements:
- A form with one input question (age) and one single select question (gender - male or female).
The created workflow will include:
- A Start point
- A Script activity
- A Form activity
- An End point
Only the settings directly connected to the Script Activity will be included in this example. If you need more information about how to set up the other workflow elements, please refer to the documentation.
Start by creating a workflow and add a Start point, a Script Activity, a Form Activity and an End point to your workflow.
In the Start Point properties please select to add BankID authentication upon starting the workflow and enter the preferred activity name and URL path name.
Then select the Script Activity in the canvas. For this workflow we have chosen to block the progress of the workflow if our script execution fails.
You may enter the script directly in the script activity properties, but if you wish to enter a longer script it can be beneficial to open the code mirror popup.
In the popup switch to code and enter the preferred groovy script.
The script we are using in this example is the following:
def birthYearShort = Integer.valueOf(ssn.substring(4,6)) def birthYear = (Util.currentDate().year - (1900 + birthYearShort) > 100) \ ? 2000 + birthYearShort \ : 1900 + birthYearShort def birthMonth = ssn.substring(2,4) def birthDay = ssn.substring(0,2) def birthDate = Util.newDate("${birthYear}-${birthMonth}-${birthDay}") def hadBirthdayThisYear = Util.getDatePlusYears(birthDate, Util.currentDate().year - birthYear) .compareTo(new DateObj(Util.currentDate())) <= 0 age = hadBirthdayThisYear \ ? Util.currentDate().year - birthYear \ : Util.currentDate().year - birthYear - 1 gender = Integer.valueOf(ssn.substring(8,9)) % 2 == 0 ? 'female' : 'male'
After entering the script, press Ok to save the script and close the code mirror popup.
In this script above we are using 3 variables we need to define:
Input variable: ssn
Output variables: "age" and "gender"
First we will map the input variable to the ssn from the start point.
Press Input Variables to add the variable.
'
Add a new variable and name it "ssn".
Then select Value From Workflow and Browse.
Expand the start point activity and select the SSN.
You will see the selected value for the input variable. Press Ok to save the changes and close the popup.
We also need to define the output variables used in the script. Press Output Variables to open the output variables popup.
Add a variable and name it gender. Our gender question should have the data type string and hence we will leave the data type with the default data type String.
Add the second output variable used in the script age. The age variable will should have the data type integer and you will have to change the data type to Integer.
Our variables have now been defined and we can move on to using the output variables to pre populate question answers.
Select the Form Activity in the canvas.
After adding your form to the form activity in the workflow, press Pre Populate Questions.
Then Select Question From Model.
Select the first question and press Ok.
The selected question will appear in the list of questions, and you may connect it to the script activity output variable, by selecting it from the list. Then select Value From Workflow and Browse.
Expand the Script Acitivty and select the output variable you wish to use for the selected question.
Then repeat the previous steps to connect the second question in the form with the second script activity output variable.
When both questions are connected to the output variables you can see them in the list of questions and values.
To complete the workflow configure the settings for the remaining workflow activities. Remember to Save the Draft, create a new version of your workflow and start the workflow from the Organizer to set it in production.
Example workflow requires Compose version 5.17.1 or higher: Script Activity with input variables