- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
10-24-2022 02:52 AM - edited 04-17-2024 01:47 AM
Workflow Automation CoE > Flows > Best 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
- Create, set, and use runtime variables.
- Apply inline transforms to data pills.
- 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
- 3,122 Views