servicenow client script interview questions
What is a Client Script, and what are the different types of Client Scripts?
A Client Script is a JavaScript code that runs on the client-side, typically in a user's web browser. It is triggered by specific client-side events, such as when a form is loaded, when a form is submitted, or when a field value is changed. Client Scripts allow you to enhance user interactions by executing custom logic in response to these events.
There are four main types of Client Scripts:
1. OnLoad: Executes when the form is loaded in the browser. It is commonly used to set default values, modify the form layout, or hide/show fields.
2. OnSubmit: Runs when the form is submitted. It is used to validate form data or perform additional actions before the form is saved.
3. OnCellEdit: Triggers when a cell in a record is edited (typically in list-type forms). It allows you to perform actions based on the changes made to the data in the cell.
4. OnChange: Activates when the value of a field changes. This script is useful for dynamically updating other fields, performing validation, or applying conditional logic based on the new value.
Can we execute an OnChange Client Script during form load? If so, how?
Yes, an OnChange Client Script is always triggered during form load. However, the script often includes a condition that prevents further execution of script if the form is in "loading" state. This is typically controlled by a variable like isLoading, which indicates whether the form is in the process of loading.
To execute the OnChange script during form load, you can modify the script to remove or adjust this check. Here's how you can achieve this:
1. Locate the condition that checks if the form is loading that is condition with isLoading variable.
2. Remove or modify this condition so that the OnChange logic is triggered regardless of the form's loading state.
Sample client script for reference:
How can you allow a user to update the incident state but restrict them from closing it in the list view? OR explain the OnCellEdit Client Script with a real-time use case.
Example Use Case:
In this scenario, we want to allow the user to update the incident state but prevent them from closing the incident directly through the list view (i.e., preventing the state from being set to "Closed").Below is an example client script which checks if new state value is closed, if yes then don't update the record by sending 'false' parameter to callback function which prevents record update else send 'true' to callback function which will update the record.
Solution
You can create an OnCellEdit Client Script on state field that checks if the new state value is "Closed." If the user tries to close the incident by changing the state to "Closed," the script will block the update. If the new state is anything other than "Closed," the record update will proceed as normal.
function onCellEdit(tableName, record, columnName, oldValue, newValue, callback) {
if (newValue === 'Closed') {
// Prevent update if the state is being set to "Closed"
callback(false); // Pass 'false' to prevent update
} else {
// Allow update if the state is not being set to "Closed"
callback(true); // Pass 'true' to allow update
}
}
Explaination
The script checks whether the new value is "Closed." If the state is being set to "Closed," the callback(false) prevents the record from being updated. If the state is not being set to "Closed," the callback(true) allows the record update to proceed.
What are the different ways to access server-side data in a Client Script?
1. Using the getReference() method:
This method is used to retrieve related record data from the server. It allows you to fetch reference field data asynchronously in a Client Script. For example, if you have a reference field in a form, you can use getReference() to fetch additional data related to that reference field.2. Using GlideAjax:
GlideAjax is a powerful way to call Script Includes from a Client Script. It allows you to send data to the server and retrieve the result asynchronously. This method is useful when you need to perform complex server-side logic or access large sets of data that cannot be efficiently handled directly on the client-side.3. Using display Business Rule (BR):
A Display Business Rule can be used to execute server-side logic and return data to the client-side when a form is loaded. This is useful when you want to perform server-side actions, such as data population or validations, during form load phase.Note:
While it is technically possible to use the GlideRecord API directly in Client Scripts to query server-side data, this is not recommended by ServiceNow. Using GlideRecord in Client Scripts can impact performance and may lead to issues with health scan reports, as it bypasses the platform's recommended data access patterns for client-side scripts.In summary, for accessing server-side data, the best practice is to use getReference(), GlideAjax, or a Display Business Rule, as these methods are optimized for client-server communication and adhere to ServiceNow's best practices.
What is the order of execution between UI Policies and Client Scripts?
If there is conflicting logic between a Client Script and a UI Policy, the logic defined in the UI Policy takes precedence and is applied. In cases where both are used to manipulate the same field or form behavior, it's important to carefully plan the logic to avoid conflicts and ensure the desired outcome.
What are the different ways to make field mandatory?
In ServiceNow, there are several ways to make a field mandatory, each with its own use case:
1. Client Script
You can use a Client Script (specifically, an onChange or onSubmit Client Script) to dynamically set a field as mandatory based on certain conditions. This allows for client-side validation and flexibility in how and when the field becomes mandatory.
2. UI Policy
UI Policies can be used to make fields mandatory based on form interactions or conditions. UI Policies are easier to manage than Client Scripts for simple use cases and automatically update the form when conditions are met.
3.Data Policy
Data Policies are similar to UI Policies but are designed to enforce field rules at both the client and server levels. These are particularly useful for ensuring data consistency across records, regardless of how the data is being submitted (e.g., via the UI, API, or import sets).
4.Field Dictionary Level
You can set a field as mandatory at the dictionary level, which ensures that the field is always required whenever a record is created or updated, regardless of the context in which it is used. This is a permanent, system-wide setting for the field.
Each of these methods can be used independently or together, depending on your requirements for field validation and business logic.
1. Client Script
You can use a Client Script (specifically, an onChange or onSubmit Client Script) to dynamically set a field as mandatory based on certain conditions. This allows for client-side validation and flexibility in how and when the field becomes mandatory.2. UI Policy
UI Policies can be used to make fields mandatory based on form interactions or conditions. UI Policies are easier to manage than Client Scripts for simple use cases and automatically update the form when conditions are met.3.Data Policy
Data Policies are similar to UI Policies but are designed to enforce field rules at both the client and server levels. These are particularly useful for ensuring data consistency across records, regardless of how the data is being submitted (e.g., via the UI, API, or import sets).4.Field Dictionary Level
You can set a field as mandatory at the dictionary level, which ensures that the field is always required whenever a record is created or updated, regardless of the context in which it is used. This is a permanent, system-wide setting for the field.Each of these methods can be used independently or together, depending on your requirements for field validation and business logic.
What is the purpose of the "Isolate Script" checkbox in Client Scripts?
If you want the Client Script to perform DOM manipulation, you should uncheck this checkbox. This allows the script to execute JavaScript that interacts with HTML elements, such as displaying alerts or modifying page elements dynamically.
DOM Manipulation Code Example
alert("Test message");
document.getElementById("elementId").style.color = "red";
Note:
DOM manipulation refers to any JavaScript code that interacts with HTML elements on the page, such as changing styles, displaying popups, or accessing form fields directly. However, DOM manipulation is generally not recommended as a best practice in ServiceNow Client Scripts. It can lead to performance issues, inconsistencies across browsers, and challenges with maintainability.
How to access dot walked field value in Client Script?
There are few fields which are not part of current table, they are brought from another reference field table.
e.g. In below example, highlighted field is Caller Email which is brought from Caller reference that is from USER table.

So, to access value from such field, we can use below syntax in client script:
g_form.getValue("caller_id.email");
Which all client script gets executed when we change field value via a list view?
You might expect that OnChange or OnSubmit Client Scripts would also execute when a field is updated, as they typically run when a field value is changed or a form is submitted. However, OnChange and OnSubmit Client Scripts are designed to run on forms, not in list views. Therefore, they do not execute when a field is updated in a list view.
When should we use a Client Script versus a UI Policy?
UI Policies are ideal for scenarios where you need to make fields mandatory, read-only, or conditionally hide/show fields based on certain conditions. They offer an easy way to enforce field behaviors without writing complex scripts.
Client Scripts, on the other hand, are more versatile and allow you to write advanced scripting logic. They are suitable for tasks like dynamically setting form field values based on complex conditions, retrieving server-side data on the client-side, or performing other custom client-side processing.
What is the g_form object in ServiceNow, and can you provide five common methods of the g_form object along with their usage?
Here are five common methods of the g_form object and their usage:
1. g_form.setValue(fieldName, value)
Sets the specified field's value to the given value.Usage: To programmatically set a field value on the form.
Example: g_form.setValue('priority', 2);
2. getValue(fieldName)
Retrieves the current value of a specified field.Usage: To get the value of a field on the form. Example: var priorityValue = g_form.getValue('priority');
3. g_form.addInfoMessage(message)
Displays an informational message at the top of the form.Usage: To show a non-intrusive informational message to the user. Example: g_form.addInfoMessage('Your changes have been saved successfully.');
4. g_form.showFieldMsg(fieldName, message, type)
Displays a message on a specific field. The message can be informational, error, or warning. Usage: To display a message next to a specific field, often used for validation or instructional messages. Example: g_form.showFieldMsg('short_description', 'Please enter a valid description.', 'error');5. g_form.addOption(fieldName, choiceValue, choiceLabel)
Adds a new option to a choice list field. Usage: To dynamically add a new option to a choice field (e.g., drop-down menu). Example: g_form.addOption('category', 'new_option', 'New Category');
How can we prevent form submission using client script?
We can prevent form submission by using the "return false" statement in an "onSubmit" client script. This method is typically used to validate the form before allowing submission.
Here is an example client script:
function onSubmit() {
// Custom validation logic
if (g_form.getValue('some_field') == '') {
alert('Some field must be filled!');
return false; // Prevent form submission
}
// If validation passes, the form will submit normally
return true;
}
How to execute a client script for a specific view? What does the 'Global' checkbox mean in a Client Script?
When we uncheck the 'Global' checkbox, a new field labeled 'View' becomes available. In this field, we can specify the name of the view for which we want the client script to execute. This allows us to tailor the behavior of the script for different views, ensuring that the script runs only when needed.
Which all objects we can access in client script?
In client scripts, we can access the following key objects to interact with the form and user data:
1. g_form: This object is used to interact with form fields, retrieve or set field values, manipulate field visibility, read-only status, and other form-related properties.
e.g. g_form.setValue('field_name', 'new_value');
2. g_scratchpad: This object is used to store temporary data on the client side that can be passed from server-side scripts (like Business Rules or Script Includes) to the client-side. It is useful for sharing data between server and client without modifying the form fields.
e.g. g_scratchpad.someData = 'value from server-side script';
3. g_user: This object provides information about the currently logged-in user, such as their username, roles, and other user-related data. It's helpful for conditionally controlling the behavior of client scripts based on the user's identity or role.
e.g. var currentUser= g_user.getUsername();
Real Time Sample Questions:
1. Explain any complex client script that you have created for given requirement?
2. Did you learn anything interesting while working on client script which is not common or documented?
3. Did you ever face any issue/challenges while using GlideAjax call in client script?
4. If you used GlideAjax method to get server side data then why used GlideAjax, why not getReference or Display BR?
Assignment for you:
1. How to send more than one variable from server side script to client side while using GlideAjax?
2. Why we won't be able to stop form submission while validating form data via GlideAjax call?
3. Explain each step that needs to be done on script include side to use it for GlideAjax?
Fuel My Passion
Loving the content? Well, of course you are. Here’s your chance to indirectly fuel the chaos that keeps this website running. Your contribution helps keep the wheels turning and allows me to continue pretending to be a responsible adult—while cranking out more content for you. Thanks for supporting my delusional dreams and helping me keep this website alive!
Buy Me a Coffee
Support with UPI
If you prefer making a UPI payment to support the website maintenance cost, scan the QR code below:

User Added Interview Question and Answers
What is interceptor page in Servicenow? What are the uses of it?
Difference b/w setVisible() and setDisplay()? - they both with hide or show the field but setDisplay will hide and reclame the space while setVisible will simply just hide that field.
can anyone explain Rest API PUT method ? i want to modify every incident in source instance that's need to be reflected in target instance. for each record we can give sys_id of the record what about all records ?
Please add the questions related to HRSD and CSM As well .
Can someone please the examples of client scripts. Like real time requirements where client scripts are created
Why g_form object will not work in onCellEdit Client Script?
Guys, In my Interview They asked like What complex Scenario you have worked on, how you have troubleshoted and if it not solved by you then by whom you get help ? Please answer
Let us consider there is checkbox field with 3 values. out of 3 checkbox field user should select atleast 2 checkbox. incase, if user did not selected 2 checkbox then error should display
I was asked what is the difference between getXML() and getXMLAnswer()?
Complex example of client script, or the most complex client script you wrote, this interview question is asked in almost all the interviews?
What will happen if we use GlideRecord in client script?
The interviewer asked if we need Email in the email field when user field is filled it automatically populated in the INC form. So, I said we can achieve by the client script we use getrefrence field (Sys_ID) he needs more info so please answer it
interviewer asked me , what are the backend tables, i said incident l, probelm, but he said no. let me know what are the backend tables.
When we will use client script and script include with glide Ajax.Please can you explane with one use case
4. If you used GlideAjax method to get server side data then why used GlideAjax, why not getReference or Display BR?
How to update change state from "Scheduled" to "Implement" automatically when Planned start date is reached?
tell me some limitations and considerations when using client scripts in ServiceNow.
Can you describe a scenario where you used a client script to enhance the user experience in ServiceNow? What was the problem you were trying to solve, and how did the client script address it?
What is the difference between synchronous and asynchronous client scripts? When would you use each type?
How do you debug client scripts in ServiceNow? Can you explain the techniques or tools you would use?
If we are making the field read only with client script , can ITIL user can edit from list view ?
Can you call a business rule through a client script?
What are the features you like and don't like in client scripts? What do you think would make it even better if X were given in a certain way?
A group have only one member and I want to send the approval to it's dedicated member and if he does't exists,then send aproval to manager's manager How should I achive this through flow designer or BR
I was asked what is difference between Client scripts and Catalog Client Scripts.. can anyone please post the answer for it
How we can achieve this scenario If approval is triggered to requester's manager and if he does not approve for 3 days then it should pass to manager's manager
I was asked, what is the difference between setDisplay and setVisible method. I said, setVisible maintains space even if field is hidden and with setDisplay, we won't have space in between the hidden fields. But Interviewer asked me to explain it in more technical way, can somebody please help me to understand how this could be answered in more technical way?
what are assignment rules?
🚀 Power Up Your ServiceNow Career
Join a growing community of smart ServiceNow professionals to stay ahead in interviews, sharpen your development skills, and accelerate your career.