ServiceNow JavaScript Interview Guide
ServiceNow JavaScript Interview Guide
To call a Script Include from a Client Script, first instantiate the GlideAjax class within the Client Script: 'var ga = new GlideAjax('MyScriptInclude');'. Next, use `addParam` to specify a method by name: 'ga.addParam('sysparm_name', 'getUserDept');'. Finally, use `getXMLAnswer` to send the request and handle the response with a callback function, e.g., 'ga.getXMLAnswer(function(resp){ alert(resp); });'. Simultaneously, define the Script Include inheriting from 'AbstractAjaxProcessor', specifying the method to be called ('getUserDept' in this case) which returns the necessary data .
For server-side scripts, ServiceNow provides `gs.log()` and `gs.info()` for logging information to the server logs, which aids in monitoring script executions. For client-side scripts, `console.log()` and `alert()` are used to output messages or variable values directly to the Console or as a popup. Additionally, more sophisticated debugging can be accomplished using Background Scripts or the Script Debugger, which allows stepping through code execution on the server side .
GlideAggregate is used in ServiceNow for performing aggregate functions like COUNT, SUM, and AVG on records of a table. A typical use case is when you need to calculate total incidents or sum all hours logged for a specific project. For example, to count the number of incidents, you would initialize a GlideAggregate on the 'incident' table, add a ‘COUNT’ aggregate, execute the query, and retrieve the result with: agg.getAggregate('COUNT').
In ServiceNow scripting, GlideDateTime handles date and time operations like manipulating and comparing dates. A typical use might be to calculate the difference between two dates or to format dates according to a specific pattern. GlideSystem (gs) provides utility functions such as logging, generating unique identifiers, or fetching user and system properties. For example, `gs.info()` is used to log messages to the system log for debugging purposes while GlideDateTime can be used to convert date formats .
Script Includes in ServiceNow promote modularity and reuse by encapsulating logic that can be invoked in different scripting contexts, such as Business Rules, Client Scripts via GlideAjax, or REST API calls. This prevents code duplication, simplifies maintenance, and enhances organization by separating business logic from the UI logic. It also allows changes to be centralized, reducing error risk across reusable logic .
The GlideRecord API in ServiceNow is used for basic database operations such as querying, inserting, updating, or deleting records. A typical workflow involves creating an instance of GlideRecord for a specified table, adding filters using `addQuery`, and calling `query()` to retrieve records. The results can be iterated over with `while (gr.next())`, allowing access to each record's fields . For example, retrieving all high-priority incidents could be done by querying 'incident' table, filtering by priority, and iterating over the results to access their properties.
Client-side scripts in ServiceNow include onLoad, which executes when a form is loaded; onChange, triggered when a specific field value changes; onSubmit, which runs before a form submission to, potentially, prevent it; and onCellEdit, applied to list cells upon editing. Each script type is designed to enhance form interaction directly in the user’s browser, enabling a responsive and dynamic user interface .
GlideAjax in ServiceNow is designed to facilitate asynchronous client-server communication. It allows a Client Script to call server-side code, avoiding full page reloads. This is done by instantiating a GlideAjax object, specifying a Script Include and method parameters with `addParam`, then using `getXMLAnswer` to execute and handle the server response via callback function in a non-blocking way. This enables efficient data exchange and real-time updates in the user interface .
In ServiceNow, g_scratchpad is used to pass data from server-side to client-side scripts. Data is added to g_scratchpad in a Display Business Rule, which runs before the page renders; the data can then be accessed in the Client Script by referencing g_scratchpad directly . This helps seamlessly transfer server-calculated data to the client side for UI manipulations.
Client Scripts run on the browser and are used for handling form and UI actions. They execute as soon as the user interacts with a form, either on load, change, submit, or cell edit . In contrast, Business Rules are server-side scripts that operate on database operations ensuring data integrity and enforcing business logic during create, update, or delete activities .