Workflow scripts

Workflows use a number of activities to build up a process in ServiceNow. These activities can often be used on a basic level, without the need for scripting. However, to build a more advanced workflow, we can use code to enhance the workflows we build.

Scripts can appear in a number of workflow activities, including approvals and task creation. For approvals, it is possible to add users and groups to approval activities using script. In task creation, we can use script to set values on our task before it is created.

The main area for scripting in a workflow, though, is in the Run Script Workflow Activity. This activity allows the developer to run any server-side script they like at a point in the workflow. This can be used to manipulate records, kick off integrations, or perform other outcomes using scripts.

The other area of workflows in which you often come across scripts is in setting up approvals. A simple approval workflow activity will simply pick a user or group to approve, or perhaps the value of a particular field. However, if you require the selection of relevant approvals to be more complicated than this, then you may need to use some code.

With scripts, you can add extra elements to who will be selected to approve, for example, by checking attributes about the users approving, or the record being approved. It may be that if a record has a particular category, it is sent to a certain approval group, or that the approver of the record must have a certain role. This type of functionality is achievable with script and can cater for many other scenarios too. 

The potential for building a complex approval system is huge, but be careful to make sure that what you create provides value and can be maintained.

Let us have a look at an example of a workflow script in a Run Script Workflow Activity:

//Adds urgent to the short description if the priority is critical
if (current.priority < 2) {
current.short_description = current.short_description + ' URGENT';
current.update();
}

In the example, we are adding URGENT to the short description of the record if the priority is critical. We also have access to the current record in the workflow script, with the current record being the one from which the workflow launched.

Let us have a look at what this activity will look like, in Figure 6.3:

Figure 6.3: Workflow activity to update the short description based on priority

Workflow script can be particularly handy for adding an extra layer of functionality to your workflows. Sometimes, when you look at the activities available, you cannot find one that will meet your requirements, and in this instance, scripting can be the solution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.143.181