Scripting best practices
Quick answer
Follow these to keep code performant and maintainable.
Follow these to keep code performant and maintainable.
- Query only the fields and rows you need; add
setLimit(). - Never run a GlideRecord query inside a loop over another query.
- Use
GlideAggregatefor counts and sums. - Centralise reusable logic in Script Includes, not copied business rules.
- Prefer configuration and out-of-the-box features to stay upgrade-safe.
- Guard against recursive business rules.
- Cover critical logic with ATF tests before promoting update sets.
Want to learn this properly?
Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.
Check your understanding
What should you avoid inside a query loop?
- A. Another GlideRecord query
- B. A gs.info call
- C. A variable
Show answer
A. Another GlideRecord query
Nested queries multiply database calls.
Where does reusable logic belong?
- A. A Script Include
- B. Copied business rules
- C. A client script
Show answer
A. A Script Include
Script Includes centralise reuse.
Frequently asked questions
What does the term Scripting best practices refer to in ServiceNow?
Follow these to keep code performant and maintainable.
What is another point to note about Scripting best practices?
Never run a GlideRecord query inside a loop over another query. Use GlideAggregate for counts and sums. Prefer configuration and out-of-the-box features to stay upgrade-safe.