Using Code block

Studio has a range of different tasks that you can use to build different processes. Being a no-code application you would not commonly be using any kind of code or any variables and functions. However, there would be times when you can achieve just the expected output using your custom code block; pass parameters and use the values duly returned by the code block.

Studio provides the Code block which can be used to create the code block that can be used anywhere in the workflow. You can pass the parameter to the functions that you are using. You can add the Javascript code which can return the values to other function calls on your screen.

First, let us have a look at this video to have a quick understanding of how to use a code block.

Now let us take a scenario to understand how to add a code block. Let us assume that we are accepting the travel details of a user. The user would enter the city that he wants to travel to and the date on which he is planning travel. An email has to be sent to the user stating the city and day whether it is a weekend or not.

Now let us now first add from the Select Task as Code Block and click Continue. Enter a self-explanatory name and description and click Next.

Now add the codeblock details which include the Parameter (if any) and the Java script for the action to be performed. The output of this code block would be used in the other tasks as Taskname.OUTPUT.

For the example here we would be taking the Travel date to pass it as a parameter to the function/code block IsWeekend() that we have added to find whether the day is a Weekend or not. So now add a parameter Name and then assign the value that would be passed, which in this example is the Travel Date selected from ‘traveldate’ which is the Datepicker control. You can then add your javascript and use ‘return’ to assign the value of the function to the IsWeekend.OUTPUT.

Once you have added the code you can test it by adding the Test Value. After you have ensured that the code block contains no syntax or logical error you can click Done.

You can make the function Asynchronous mainly to optimize the long-running workflows.
You would then use the resolve or reject to return the values.
You can also view the supported libraries.

Now that you want to send the email to the User, stating whether the selected Travel Date is a Weekend or not, you need to add an Email Task.

The Email task that you define would be using the OUTPUT of the code block. In this example we are returning the “is a weekend” or “is not a weekend” as the output of the code block. Hence we have added a variable {{weekend}} to fetch the value returned by the code block using {{weekend}} = IsWeekend.OUTPUT. The variable will be used to display the text in the email as shown below.

Once the email task’s Sender details are defined, you should make sure that the workflow is linked to the Submit button. You may have Update tasks or other tasks as required for your process.

When the form is executed, the travel date and location would be entered. When you click Submit, the Workflow would be triggered. The selected date would be sent as a parameter to the code block and the user would receive an email with the details whether the day is a weekend or not.

Travel date selected is 16th August 2020, which is a Sunday. Hence the email would be as follows:
preview code task

Now if the date selected would have been 18th August 2020 which is a Tuesday, the email would be sent as follows:

travel 2

Thus you can use the code block to carry out such custom functions that are specific to our use case. It can be effectively used by passing appropriate parameters and using the return values for further action.

The Code block variable however, cannot be used outside the code block. The values returned are also a part of the task where the function is getting called.