World of Client-side Scripts #2
- Replace the GlideRecord and getReference with an asynchronous GlideAjax call.
- The isLoading flag should be used at the beginning of any script which is not required to run when the form is loading.
- Add newValue checks which prevents the script from running when the value is removed or blanked out.
Enclose Code in Functions
When code is not enclosed in a function, variables and other objects are available and shared to all other client-side scripts. If you're using the same variable names, it is possible they could collide. This can lead to unexpected consequences that are difficult to troubleshoot.
This solution is much safer because the scope of the variable state is limited to the onSubmit() function. Therefore, the state variable does not conflict with state variables in other client-side scripts.
Minimize Server Lookups
There are multiple methods to bring the server-side data on the client-side, but we will talk of only recommended ways rather than all.
Recommended: - g_scratchpad and asynchronous GlideAjax call.
Non-recommended: - GlideRecord and g_form.getReference()
The primary difference between these methods is that g_scratchpad is sent once when a form is loaded, whereas GlideAjax is dynamically triggered when the client requests information from the server. Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups.
Learn more: - g_scratchpad, GlideAjax
Practices to Avoid
While scripting for Client-side Script / ServiceNow Client Script, there are two major practices to avoid which could impact more as compared to other practices:
- DOM Manipulation
- Global Client Scripts
DOM manipulation in JavaScript is the process of interacting with the DOM API to change or modify an HTML document that will be displayed in a web browser. By manipulating the DOM, we can create web applications that update the data in a web page without refreshing the page. The DOM stands for Document Object Model.
Avoid Document Object Model (DOM) manipulation if possible. It can cause a maintainability issue when browsers are updated. The only exception is when you oversee the DOM: in UI Pages, and the Service Portal. Instead, use the GlideForm API or consider a different approach for the solution.
A global client script is any client script where the selected Table is Global. Global client scripts have no table restrictions; therefore, they will load on every page in the system introducing browser load delay in the process.
As an alternative, and for a more modular and scalable approach, consider moving client scripts to a base table (such as Task [task] or Configuration Item [cmdb_ci]) that can be inherited for all the child/extending tables. This eliminates the system loading the scripts on every form in the UI - such as home pages or Service Catalog where they are rarely (if ever) needed.
Thank You!
Regards,
Kailash Bhange,
Rising | LinkedIn
https://www.servicenow.com/community/developer-blog/world-of-client-side-scripts-2/ba-p/2773618