Podcast
Questions and Answers
When writing a script that only needs to run in a single context, such as a transform map script, what type of function should you use?
When writing a script that only needs to run in a single context, such as a transform map script, what type of function should you use?
- Recursive function
- Anonymous function
- Global function
- Self-executing function (correct)
When retrieving a value from a reference field, which method should you use to avoid dot-walking to the sys_id?
When retrieving a value from a reference field, which method should you use to avoid dot-walking to the sys_id?
- `getValue("field_name")` (correct)
- `getParameter()`
- `getProperty()`
- `getDisplayValue()`
Why is it recommended to use getDisplayValue()
instead of getValue("field name")
when retrieving a field's value for display?
Why is it recommended to use getDisplayValue()
instead of getValue("field name")
when retrieving a field's value for display?
- It is faster and more efficient.
- It prevents script errors when the field is empty.
- It automatically formats the value based on the field type.
- It reduces the need for code changes if the display value changes. (correct)
In client scripts, what is the primary difference between using g_scratchpad
and a GlideAjax lookup to get information from the server?
In client scripts, what is the primary difference between using g_scratchpad
and a GlideAjax lookup to get information from the server?
Why should you avoid using current.update()
in a Business Rule script?
Why should you avoid using current.update()
in a Business Rule script?
What is the recommended way to debug server-side scripts in ServiceNow?
What is the recommended way to debug server-side scripts in ServiceNow?
When should you use Async Business Rules instead of After Business Rules?
When should you use Async Business Rules instead of After Business Rules?
What is the primary benefit of using Script Includes over Global Business Rules?
What is the primary benefit of using Script Includes over Global Business Rules?
When using setValue()
on a reference field in a form, why is it important to include the display value with the sys_id
?
When using setValue()
on a reference field in a form, why is it important to include the display value with the sys_id
?
In client scripts, what conditional check should be included in onChange
client scripts to ensure they run only when necessary?
In client scripts, what conditional check should be included in onChange
client scripts to ensure they run only when necessary?
When working with GlideRecord to count rows, which method should you use to avoid scalability issues as tables grow over time?
When working with GlideRecord to count rows, which method should you use to avoid scalability issues as tables grow over time?
What should you do before trying out a new script in staging or production instances?
What should you do before trying out a new script in staging or production instances?
When writing Jelly code, what should you avoid using to prevent performance issues?
When writing Jelly code, what should you avoid using to prevent performance issues?
If you have multiple client scripts on a single table, what should you ensure to control their execution order?
If you have multiple client scripts on a single table, what should you ensure to control their execution order?
Instead of client scripts, what should you use to set field attributes such as mandatory, visible, and read-only?
Instead of client scripts, what should you use to set field attributes such as mandatory, visible, and read-only?
What is the potential impact of using Global Client Scripts?
What is the potential impact of using Global Client Scripts?
When debugging on the client side, which tools can you use to view logs and identify issues?
When debugging on the client side, which tools can you use to view logs and identify issues?
Which type of messages should you search for in the Node Log File Download to diagnose potential system issues?
Which type of messages should you search for in the Node Log File Download to diagnose potential system issues?
What does the ECC Queue primarily control?
What does the ECC Queue primarily control?
Why is it important to review the Event Logs and sort by Processing duration in descending order (Z-A)?
Why is it important to review the Event Logs and sort by Processing duration in descending order (Z-A)?
Flashcards
Add comments to your code
Add comments to your code
Add explanatory phrases within the code to clarify its functionality and purpose.
Use Whitespace and empty lines
Use Whitespace and empty lines
Employing whitespace and empty lines to delineate logical sections, making code visually easier to parse.
Simplify conditional statements
Simplify conditional statements
Favor straightforward if/else
statements over complex ternary operators for improved readability.
Modular code components
Modular code components
Signup and view all the flashcards
Construct reusable functions
Construct reusable functions
Signup and view all the flashcards
Descriptive variable and function names
Descriptive variable and function names
Signup and view all the flashcards
Verify variable value before using
Verify variable value before using
Signup and view all the flashcards
Consistent return types
Consistent return types
Signup and view all the flashcards
Avoid Eval function
Avoid Eval function
Signup and view all the flashcards
Use jelly variables
Use jelly variables
Signup and view all the flashcards
Use addEncodedQuery
Use addEncodedQuery
Signup and view all the flashcards
Use GlideAggregate for record count
Use GlideAggregate for record count
Signup and view all the flashcards
Add setLimit(1)
Add setLimit(1)
Signup and view all the flashcards
Self Executing Functions
Self Executing Functions
Signup and view all the flashcards
Avoid hard coded values
Avoid hard coded values
Signup and view all the flashcards
Client Script Condition
Client Script Condition
Signup and view all the flashcards
System property debug
System property debug
Signup and view all the flashcards
Business Rules conditions
Business Rules conditions
Signup and view all the flashcards
Study Notes
Overview of Server-Side Scripting Best Practices
- Make code easy to read by including comments and using whitespace and empty lines
- Use
if/else
statements instead of ternary operators for better readability - Break code into modular components with specialized functions
- Construct reusable functions for similar logic
- Use descriptive variable and function names
- Store function results in a variable to reuse and avoid calling the same function multiple times
- Verify that the value of a variable exists before using it
- Return a value when creating functions
- Avoid using the
Eval
function due to security risks and debugging challenges - Avoid dynamic JEXL expressions inside Jelly tags (
<g2:evaluate>
) as they can affect memory resources and cause performance issues
Interacting with the Database
- When using Jelly, use
jelly.jvar_user_id
instead of${jvar_userid}
- Use
addEncodedQuery()
instead of complexgliderecord
queries with multipleaddQuery()
andaddOrCondition()
- Use
GlideAggregate
for efficient record counting instead ofgetRowCount()
fromGlideRecord
- Avoid complex queries on large datasets to prevent performance issues
- Utilize the database for data processing and filtering instead of client or server-side scripting
- Use self-executing functions for scripts that only need to run in a single context
Avoiding Coding Pitfalls
- Always test code in a sandbox environment before deploying to staging or production
- Avoid hard-coding values like sys_ids in scripts; use system properties and
gs.getProperty
instead - Instead of dot-walking to get a reference field's sys_id, use
current.getValue("caller_id")
- Use
getDisplayValue()
instead ofgetValue("field name")
to avoid code changes if the display value changes
Client-Side Scripting Best Practices
- Use client scripts to validate data
- Ensure the correct script order for multiple client scripts on a table, where lower order values execute first
- Use UI policies to set field attributes like mandatory, visible, and read-only instead of client scripts
- Restrict list editing to avoid conflicts between client scripts/UI policies and list editing
- Only run necessary client scripts to avoid performance issues
- Use
newValue != oldValue
in onChange client scripts - Ensure isLoading and newValue checks exist in onChange Client scripts
- Call GlideAjax only when necessary, checking values beforehand to avoid unnecessary server lookups
- Enclose code in functions to prevent variable collisions
Minimizing Server Lookups
- Use client-side data whenever possible to reduce server lookups
- Use
g_scratchpad
and asynchronous GlideAjax lookups to get information from the serverg_scratchpad
is sent once when a form is loaded, while GlideAjax is triggered dynamically
- Avoid GlideRecord and g_form.getReference() callbacks due to performance impact
- When using
setValue()
on a reference field, include the display value to avoid synchronous Ajax calls
Client Scripting Practices to Avoid
- Avoid Global Client Scripts, as they load on every page and cause browser load delays
- Avoid DOM Manipulation, except in UI Pages and the Service Portal, and use GlideForm API instead
Business Rules Best Practices
- Ensure the
when
field is carefully considered when writing Business Rules - Use Async Business Rules instead of
after
Business Rules if the updated values do not need to be immediately shown - Use Conditions in Business Rules to avoid unnecessary evaluations
- Enclose code in functions to avoid variable conflicts
- Prevent Recursive Business Rules by avoiding
current.update()
in a Business Rule script - Use Business Rules to double-check critical inputs
- Use Script Includes instead of Global Business Rules, as Script Includes load only when called
Debugging Best Practices
- For server-side debugging, use the debug navigation filter
- Use System Diagnostics > Debug Log to display
gs.debug()
,gs.info()
,gs.print()
, andgs.log()
statementsgs.print()
andgs.log()
are unavailable in scoped applications
- System Diagnostics > Debug Business Rule displays messages about business rules
- System Security > Debug Security Rules places a debug icon on each field to display debug messages
- Use
gs.debug()
statements controlled by system properties for server-side debugging - For client-side debugging, enable JavaScript Log and Field Watcher
- Use
jslog()
statements in client scripts and UI policy scripts - Check the browser's console log for additional debugging information
- Disable server-side debugging before closing an update set or completing testing
Logs and Queues
- Review warnings and errors in logs and queues frequently
- System Logs > System Log > All shows all warnings and errors
- Regularly check the size of log files via System Logs > Node Log File Download.
- Review recent log files (files with names that start with localhost_log) and search for messages that may indicate potential issues:
- Slow evaluate
- Slow Business Rule
- Recursive Business Rule
- Compiler exception
- Warning - large table limit
- Extremely large result
- Diagnose performance issues by reviewing the Slow SQL Statements log
- Check the ECC Queue to identify any problems with the instance or MID Server
- Reviewing Event Logs records all system events that occur within ServiceNow
- Improve overall system performance by removing unused events
Update Set and XML Data Transfer
- Use a well-defined migration process for moving changes between instances
- Create an ordered list detailing how grouped update sets should be applied
- The migration procedure should include information about any additional data or configuration changes required to accompany the update sets
- Need instructions for:
- Activating plugins
- Configuring system properties or other instance-specific settings
- Setting up MID Servers and identity servers
- Ensuring that the target table is available
- Export XML data from one instance and import it to another to migrate data
- Migrating data in an XML file saves time for unscheduled data imports since there is no need to build an import set or a transform map
- Adding a comment in the update set description is good practice so the user installing the update set is aware that an XML import is required
- Importing XML preserves data without firing business rules or updating the instance cache
- Only the selected record will be exported, not the records related to it
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.