0% found this document useful (0 votes)
103 views3 pages

ServiceNow JavaScript Interview Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views3 pages

ServiceNow JavaScript Interview Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ServiceNow JavaScript Interview Questions &

Answers

Basic Level

What are the types of scripts in ServiceNow?


Client-side (browser): Client Scripts, UI Policies, UI Actions (client), Catalog Client Scripts.
Server-side: Business Rules, Script Includes, Scheduled Jobs, Workflow Scripts, Background
Scripts.

What are the main Glide APIs used in ServiceNow scripting?


GlideRecord – DB operations
GlideSystem (gs) – System utilities
GlideDateTime – Date/time handling
GlideUser (g_user) – Current user info
GlideAjax – Client-server communication.

What is a GlideRecord and how is it used?


Used to query, insert, update, or delete records:

var gr = new GlideRecord('incident');


[Link]('priority', 1);
[Link]();
while ([Link]()) {
[Link]('High Priority: ' + [Link]);
}
Intermediate Level

How do you call a Script Include from a Client Script?


Client Script:
var ga = new GlideAjax('MyScriptInclude');
[Link]('sysparm_name', 'getUserDept');
[Link](function(resp){ alert(resp); });

Script Include:
var MyScriptInclude = [Link]();
[Link] = [Link](AbstractAjaxProcessor, {
getUserDept: function() { return 'IT'; }
});

Difference between Client Script and Business Rule?


Client Script: Runs on the browser for form/UI actions.
Business Rule: Runs on the server for DB operations.

What are the types of Client Scripts?


onLoad, onChange, onSubmit, onCellEdit.
Advanced Level

What is the purpose of g_scratchpad?


Used to pass data from server to client.

Display BR:
g_scratchpad.manager = [Link];

Client Script:
alert('Manager: ' + g_scratchpad.manager);

What is GlideAggregate used for?


Performs aggregate functions like COUNT, SUM, AVG.

var agg = new GlideAggregate('incident');


[Link]('COUNT');
[Link]();
if([Link]()) [Link]([Link]('COUNT'));

How do you debug scripts?


Server: [Link](), [Link]()
Client: [Link](), alert()
Use Background Scripts or Script Debugger for deeper debugging.

Common questions

Powered by AI

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 .

You might also like