In the ServiceNow, setWorkflow() is generally used to prevent the running of business rules (or other scripts) that would be triggered by any server side updates you're making.
It essentially tells ServiceNow to disregard any scripts that would be triggered by the script that you're running setWorkflow from.
setWorfklow can be used in any server-side script, including Business Rules, Script Includes, Scheduled Jobs, and (server-side) UI Actions.
The setWorkflow() method utilizes a single boolean argument of either true or false. The arguments are written as follows:
setWorkflow(false): All business rules on the table for the current scripted action (insert, update, delete, query) will be ignored.
setWorkflow(true): All business rules on the table for the current scripted action (insert, update, delete, query) will be executed.
Next, let's look at an example of how to use setWorkflow in ServiceNow.
In the following example scenario, the Network Team heavily utilizes the Incident Management application and has requested a new business rule that automatically updates the 'Urgency' of all active incidents to 'High'... but without triggering any of the business rules that normally run whenever this type of update occurs.
To perform this, we'd want to use setWorkflow(false) like in the following script:
Although the above script performed as desired, the Network Team later decided they wanted to also update the Notes of the incident with some text, then re-enable business rules so the usual Notifications would trigger upon updating the Notes field.
To perform this, let's add a setWorkflow(true) line later in the script:
By utilizing setWorkflow(false) and setWorkflow(true) in the same script, we were able to bypass business rules temporarily and then re-enable them for subsequent updates within a loop.
Keep in mind that using setWorkflow(false) bypasses all business rules. This means that any important checks, validations, or actions in the business rules will be skipped. This can lead to unexpected outcomes or cause integrity issues, so make sure that you don't need the business rules to run.
The setWorkflow() method is useful tool that allows ServiceNow developers to temporarily bypass business rules, (and if needed, re-enable them) within a script.
We hope this guide for how to use setWorkflow in ServiceNow has helped you gain a better understanding of the tool and you'll be ready to yield it next time you need it!