The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Dawn Jurek
ServiceNow Employee
ServiceNow Employee

We empower our customers to automate processes and make them run as efficiently as possible. Take client scripts, for example. In the ServiceNow system, a client script is a piece of JavaScript code that runs on the client browser. These scripts are commonly used to make forms smarter—tailor the fields to individual users, populate or validate field values, display messages, and so on.

If you're just getting started with client scripts, check out this video on our NOWSupport YouTube channel:

Client scripts can be really helpful and powerful little chunks of code, but they also have a potential dark sidethey can impact performance for the user. In this installment of our best practices series, we look at six ways you can keep client scripts from bogging down your processes.

Six ways to improve client script performance

  1. Use a UI Policy instead
  2. Reduce server lookups
  3. Avoid global client scripts
  4. Add the isLoading check to onChange scripts
  5. Add the newValue check
  6. Run server calls as late as possible within the script

Use a UI policy instead of a script

You can use UI policies to make form fields mandatory or optional, visible or hidden, or read-only or writable when certain conditions are met. It's a good idea to use a UI policy instead of a script whenever you can. UI policies execute efficiently, and unlike with scripts, you can control the order in which multiple UI policies execute. Plus they're easy to create, which saves you the trouble of writing the scripts.

ui_policies_list.jpg

And here's a tip. You can run a script as part of a UI policy, but that can be tricky to troubleshoot down the road. Stick with the supplied UI policy functions if you can.

Reduce server lookups

Retrieving data from the server takes time, so make as few server requests as possible, and retrieve only the data you need.

Another way to reduce server lookups is to use g_scratchpad (in a display business rule) and GlideAjax (in any script) instead of g_form.GetReference() and GlideRecord. The first pair are more efficient because they let you choose which fields you retrieve from the server, whereas the second pair retrieve the entire record.

Avoid global client scripts

A global client script is one where the selected table is Global. Since these scripts load for every table, they load for every page in the system, including home pages and Service Catalog pages, where they're very rarely needed.

Instead, choose a specific table for the script. If you choose a base table like Task [task] or Configuration Item [cmdb_ci], the script is loaded for pages that refer to that table or any of its child tables, but not for other pages. This helps these other pages load faster.

Add the isLoading check to onChange scripts

You typically don't need to run onChange scripts when a form loads because they would already have run the last time a value on the form changed. To keep an onChange script from running when the form loads, check the isLoading flag at the start of the script and stop the script if it's true, like this:

      if (isLoading)

              return;

Add the newValue check

Typically you don't want to run a script that references a particular field if the form doesn't contain a value for the field. To avoid this, check newValue and run the rest of the script only if it's non-null, like this:

      if (newValue) {

      // rest of script follows

Do server calls as late as possible

Often a script will check various items available on the client (like the isLoading and newValue checks we saw earlier) before running the rest of the script. Be sure to put server calls (like GlideAjax) after these checks to prevent wasting time on the server calls.

_________________________________________________________________

By following these tips, you can ensure that your client scripts behave and support your processes, rather then impeding them.

For more information, see:

--

Behind the scenes here at ServiceNow, the Knowledge Management and Multimedia teams work closely with subject matter experts to disseminate critical information to our customers. We've found that certain topics come up frequently, in the form of best practices that can help you keep your ServiceNow instances running smoothly. This series targets those topics so that you and your organization can benefit from our collective expertise. If you have a best practices topic you'd like us to cover in this series, please let us know in the comments below.

To access all of the blog posts in this series, search for "nowsupport best practices series."

1 Comment