Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Workflow Automation CoE > FlowsBest Practices > Variables and Scripting

 

Flows Best Practices Variables and Scripting

 

Inline Script

  • Limit inline scripting use to field formatting and field validation.
  • Using and transforming data at runtime is amazing for developers of all skill levels, but inline scripting should be left to experienced developers and used as a last resort when all other options prove insufficient.
  • Use transform functions when possible to format and manipulate field data using common functions rather, than inline script.

Guidelines for JavaScript

Follow general ServiceNow coding guidelines. Few of them with respect to spokes are explicitly stated here:

  • As a good practice, Script steps within an action should not grow beyond ~20 lines of script. If they do, consider breaking into separate script steps to break apart logical functions, or leverage a Script Include and call it from your action script.
  • Before pushing code, do Jslint (online code verification tool -- checks indentation, etc.) and fix comments. E.g: space before and after operators, brackets.
  • Tabs should be used instead of 4 spaces.
  • If, else, while, for block with single line statement should not be enclosed within brackets I.e., no {} for single line if or else blocks.
  • Commented or dead code should be removed.
  • Have comments around the Functions.

DO NOT USE gs.sleep(), instead use the wait for condition or wait for duration flow logic to support your use case. gs.sleep() ties up the current thread from doing other work, and if to many threads are used up, no new work occurs.

 

Using Transform and runtime data inline

LisaHolenstein_0-1666188646817.png

 

LisaHolenstein_1-1666188663916.png

 

LisaHolenstein_2-1666188673785.png

 

LisaHolenstein_4-1666188703115.png

 

  • Using and transforming data at runtime is amazing for developers of all skill levels, but inline scripting should be left to experienced developers and used as a last resort when all other options prove insufficient.
  • Transform functions are awesome for low-coders because they can be stacked, so you could first convert something to a string and then perform other string transforms.

We touched upon Flow variables earlier, but one thing I want to highlight here is that these can also be combined with inline transforms and scripting. When setting a flow variable, you can apply transforms to them or script the value to reuse later in your flow.

 

 

Center of Excellence Navigation

  1. Workflow Automation - Center of Excellence
  2. Workflow Automation Migration Considerations
  3. What's new for Workflow Automation?
  4. Resource Hubs
    1. Flows
    2. Decision Tables
    3. Playbooks
  5. FAQ
  6. Training
  7. Workflow Academy
Version history
Last update:
‎04-17-2024 01:47 AM
Updated by:
Contributors