ServiceNow Certification Mock Exams
Click To View Now
SNOWYCODE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
How To Use setWorkflow In ServiceNow
How To's

How To Use setWorkflow In ServiceNow

How To Use setWorkflow In ServiceNow

Introduction To setWorkflow

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.

Using setWorkflow()

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.

Example Scenario

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:

 var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.addQuery('assignment_group.name', 'Network');
gr.query();

while (gr.next()) {

    gr.urgency = 1; // 1 - High
    gr.setWorkflow(false);
    gr.update();

}

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:

 var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.addQuery('assignment_group.name', 'Network');
gr.query();

while (gr.next()) {

    // First update without business rules
    gr.urgency = 1; // 1 - High
    gr.setWorkflow(false);
    gr.update();

    // Second update with business rules
    gr.work_notes = 'Urgency updated to High';
    gr.setWorkflow(true);
    gr.update();
}

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.

Caution While Using setWorkflow

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.

Putting It All Together

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!

Looking for something else? Check out our other Cheat Sheets below!

Snowycode team
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Read more