What is Client Script and What are the different types of it?
Client scripts allow the system to run JavaScript on the client (web browser) when client-based events occur, such as when a form loads, after form submission, or when a field changes value.
There are 4 type of Client scripts. i. On Load ii. On Submit iii. On Cell Edit. iv. On Change.
Can we execute On Change client script while form load? if yes then how?
Yes, system by default executes on change client script while form load. However, due to the below highlighted lines in script it doesn't execute further code.If we want to execute our script onLoad of the form then we can remove those lines.
Note : Basically 'isLoading' is the variable which keeps track whether current operation is onLoad or not, we can use the same variable to execute it on change or on load based on the requirement.
How to allow user to update incident state but restrict closing it in list view? OR Explain on cell edit client script with real time use case?
The OnCell Edit client script runs when the user updates record via list view.
Example : User shouldn't be able to close the incident through list view.
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.
Below parameters are accessible in on cell edit client script.
sysIDs: an array of the sys_ids for all items being edited.
table: the table of the items being edited.
oldValues: the old values of the cells being edited.
newValue: the new value for the cells being edited.
callback: a callback that continues the execution of any other related cell edit scripts. If true is passed as a parameter, the other scripts are executed or the change is committed if there are no more scripts. If false is passed as a parameter, any further scripts are not executed and the change is not committed.
What are the different ways to access server side data in client script.
There are 3 different ways with which we can access server side data in client script.
1. Using getReference method.
2. Using GlideAjax.
3. Using display BR.
Note : We can also use GlideRecord API in client script directly but this is not recommended by ServiceNow. This also impacts health scan report.
What is the order of execution between UI policy and Client script.
UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.
What is the purpose of Isolate script checkbox in client script?
It manages whether DOM manipulation should be enabled/disabled for client script. If DOM manipulation code is not working then there is possibility that this checkbox is true which is forcing client script to run in strict mode.
Which all client script gets executed when we change field value via list?
Only on cell edit client script gets executed when we update field via list view. We might think on change or on submit client script should also execute as we are changing field and submitting record to database but these two client script executes on form only.
When we are supposed to use client script and when to use UI policy?
UI Policies are recommended to use when we want to make field mandatory,read only or hide/show fields.
Client script can be used to write quite complex scripting. e.g. Setting value on form based on some condition, getting server side data on client side etc.
Note: We can write script in UI policy also but we should be using Client script wherever possible when it comes to writing client side scripting. We are supposed to use UI policy script only when same is not possible via client script.
What is g_form object? provide 5 methods of g_form object with its usage?
The GlideForm client-side API provides methods for managing form and form fields including methods to:
- Retrieve a field value on a form
- Hide a field
- Make a field read-only
- Write a message on a form or a field
- Add fields to a choice list
- Remove fields from a choice list
Below are the common methods of g_form object :
g_form.setValue : sets value in specified field.
g_form.getValue : gets value from specified field.
g_form.addInfoMessage : Display info message on form.
g_form.showFieldMsg : display field message.
g_form.addOption : Add new option in choice field.
How to prevent form submission via client script?
We can use "return false" statement in on submit client script to prevent form submission.
How to execute client script for specific view? or What is 'Global' checkbox in Client Script?
By default 'Global' checkbox is checked while creating new client script. It means client script will execute for all views. When we uncheck it, new field 'View' will be visible, we can mention view name here to execute client script for specific view.
Which all objects we can access in client script?
We can access below objects in client script
1. g_form
2. g_scratchpad
3. g_user
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?
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
Pradeep2024-04-02 08:45:30
You can create onSubmit client script and write below script in it :
var check1 = g_form.getValue('checkbox1');
var check2 = g_form.getValue('checkbox2');
var check3 = g_form.getValue('checkbox3');
var checkArray = [check1, check2, check3];
var count = 0;
for (var i = 0; i < checkArray.length; i ) {
if (checkArray[i] == true) {
count ;
}
}
if (count < 2) {
g_form.addErrorMessage('Please check atleast 2 checkboxes');
return false;
}
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Sudheer2024-03-03 23:00:33
I was asked what is the difference between getXML() and getXMLAnswer()?
Srinithi2024-03-09 19:15:04
getXML() gives the entire XML document but getXMLAnswer() only retrieves the far more efficient Answer.
Complex example of client script, or the most complex client script you wrote, this interview question is asked in almost all the interviews?
SM2024-04-27 07:11:26
Can anyone share a sample answer for this
0 Helpfuls
Varun2024-05-28 04:21:13
There is no specific answer to this. However, client script with single/multiple GlideAjax call could be considered as complex one as it requires both server side and client side scripting. So, if you have written any such client script then you can mention it.
0 Helpfuls
2 Helpfuls
*Name is mandatory
*Please enter reply
Devarsh2024-02-12 06:04:37
What will happen if we use GlideRecord in client script?
Anshika2024-04-24 12:08:09
It works fine. However, Client-side GlideRecord is massively inefficient, far too slow, and returns way too much unnecessary data.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Rohit 2023-09-20 11:39:25
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
Laxmi2023-10-26 19:29:20
Ypu can use below script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//if(newValue != ''){
var caller = g_form.getReference('caller_id');
g_form.setValue('u_email', caller.email);
//}
}
0 Helpfuls
Pratiksha Langde2023-10-27 10:24:51
getReference is not the best practice to get data from server side to client side. we should be using GlideAjax to fetch data from server side to client side.
if you want to use getReference then you can use this code using on change client script :
unction onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var user = g_form.getreference('requested_for', function(user) {// map the correct field
g_form.setValue('u_email', user.email.toString());// map the correct field
});
}
if you want to auto-populate the email id based on logged-in user the please line in the default value of the email id field :
javascript: gs.getuser().getRecord().getValue('email);
Using GlideAjax :
Create a new client callable script include with following function
var UserDetails = Class.create();
UserDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserInfo: function() {
var details = {};
var userId = this.getParameter('sysparm_user_id');
var userObj = new GlideRecord('sys_user');
userObj.addQuery('sys_id', userId);
userObj.query();
if (userObj.next()) {
details.email= userObj.email_id.toString();
}
return JSON.stringify(details);
},
type: 'UserDetails'
});
Create a new catalog client script on the record producer/request form
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('UserDetails');
ajax.addParam('sysparm_name', 'getUserInfo');
ajax.addParam('sysparm_user_id', g_form.getValue('employee_name')); // change variable name here
ajax.getXML(doSomething);
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var answers = JSON.parse(answer);
g_form.setValue('var_email_id', answers.email.toString()); // change variable name here
}
}
0 Helpfuls
Rohit2023-11-16 05:27:34
Do we need an additional field of 'Email' to auto-populate the email address of user?
0 Helpfuls
Tanya2023-11-28 05:19:31
no
0 Helpfuls
Soumaya2023-11-28 11:41:35
If you would like to show email address of user on incident form then we can dot walk to user reference field and bring email field on task form. This way, we don't need to write any script to populate email address when user is changed.
1 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Mujahid2023-09-01 01:51:33
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.
Kapil 2023-09-19 22:16:51
The interviewer is asking about the backend name of the tables.
for example, Incident table is incident, change table is change_request,
0 Helpfuls
Feroz Khasim2023-12-05 20:18:43
For all the tables we have one table that is stored in database and the table name is "sys_db_object.LIST"
1 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Balaji2023-08-25 02:19:29
When we will use client script and script include with glide Ajax.Please can you explane with one use case
Atul2023-09-26 11:37:56
Whenever you need data in client script which is not available on the form then you will need to get it from server database. And to do so we can use GlideAjax. OR if you need to perform any operation/validation which requires server side script then we need to use GlideAjax.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Mujahid2023-08-10 19:21:28
4. If you used GlideAjax method to get server side data then why used GlideAjax, why not getReference or Display BR?
Saritha2023-08-29 12:48:11
g_scratchpad is sent once when a form is loaded (information is pushed from the server to the client), whereas GlideAjax is dynamically triggered when the client requests information from the server.
If you're using getReference you will get ALL data for each record you "get" whether you need it or not.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Sammy2023-07-27 09:42:42
How to update change state from "Scheduled" to "Implement" automatically when Planned start date is reached?
Suresh Thorati2023-09-26 07:14:18
1. Create a BR on Change Table.
a. State = Scheduled
Advanced: use glide time
0 Helpfuls
Trupti2023-10-09 11:15:32
- You can either use Flow Designer or Scheduled Job.
- Schedule it to execute daily.
- Glide into 'Change' table and check if planned start date = todays date and state=scheduled then update change state to 'implement'
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Madhuri2023-06-29 10:52:21
tell me some limitations and considerations when using client scripts in ServiceNow.
Uday 2024-01-17 11:54:08
We can use DOM manipulation.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Madhuri2023-06-29 10:44:35
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?
0 Helpfuls
*Name is mandatory
*Please enter reply
Madhuri2023-06-29 10:44:00
What is the difference between synchronous and asynchronous client scripts? When would you use each type?
Kevin2023-07-11 07:51:56
There is not exact concept called Synchronous/Asynchronous client script.
However, if you use GlideAjax (which makes async call) in client script to get data then you can say it is Asynchronous client script. And client scripts that do not use GlideAjax, you could say those are synchronous.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Madhuri2023-06-29 10:43:31
How do you debug client scripts in ServiceNow? Can you explain the techniques or tools you would use?
Tirupati2023-08-03 22:09:48
We can debug Client script by keeping alerts
var cat = g_form.getValue('catgory');
alert(cat);
.....
.....
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Manasa2023-06-29 10:40:13
If we are making the field read only with client script , can ITIL user can edit from list view ?
Rakhi2023-07-11 07:40:14
Yes, they can. We generally make fields read only via onLoad/onChage client script and it works only for form view not for list view.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Manasa2023-06-29 10:39:25
Can you call a business rule through a client script?
Imraan2023-07-11 07:46:21
You cannot call BR from client script. However, you can write Display BR to store values in g_scratchpad object and access that object in client script.
Practically there is no any scenario where you would need this. If you want any server side data in client script then you can always use GlideAjax in client script.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Manasa2023-06-29 10:37:48
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?
0 Helpfuls
*Name is mandatory
*Please enter reply
Kanika2023-06-14 03:19:42
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
Pratik2023-06-21 07:14:48
Hello Kanika,
This can be implemented easily via workflow where you can inlcude validation script to check if dedicated user exist in that group, if not, then send it to managers manger. Workflow allows us to write such quite complex logic while triggering approval. However, I am pretty sure it can be achieved via Flow Designer as well but it becomes quite difficult to implement such logic in flow designer. I would not recommend to BR for triggering approvals as it becomes difficult to track approval status to perform further operation.
0 Helpfuls
1 Helpfuls
*Name is mandatory
*Please enter reply
Sahitya2023-06-08 06:29:57
I was asked what is difference between Client scripts and Catalog Client Scripts.. can anyone please post the answer for it
Martin2023-06-09 03:37:34
Hi Sahitya,
As such there is no difference technically. However, Catalog client scripts are written for catalog items or variables sets and Client Scripts are written for backend tables e.g. incident, problem etc.
0 Helpfuls
Surendra2023-07-13 04:00:32
As Such there is no difference technically.
Client script have 4 types
Onload
OnChange
OnSubmit
Oncelledit
Catalog client script have 3 types
Onload
Onchange
Onsubmit
0 Helpfuls
Satyapriya Biswal2023-07-13 18:32:25
Catalog client scripts stored in catalog_script_client table and Client scripts stored in sys_script_client table.
In catalog client script we have the option to apply the client script to the catalog item view, RITM view , Task view and also Target record view But in Client script we does not have that options.
In Client script we can apply the script to a specific view by checking the Global checkbox But in catalog Client script there is no Global check box because Catalog client script applies on a specific catalog item or a variable set
2 Helpfuls
Anonymous2023-08-01 21:37:15
in catalog client script their is no cell edit type client script we have only 3 types i.e onload, on submitte, on change that is major diffrence in that
0 Helpfuls
2 Helpfuls
*Name is mandatory
*Please enter reply
Amit2023-05-05 22:05:42
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
Rahul2023-05-22 01:57:17
We can achieve this via workflow. While triggering approval to requester's manager, parallelly we can trigger timer activity for 3 days, preceded by approval to requester's manager manager.
If requester's manager approves within 3 days then end the workflow. If requester manager doesn't approve within 3 days then timer activity will complete and then another approval to requester's manager manager will trigger.
0 Helpfuls
Kanika2023-06-14 03:11:50
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 need to do it using flow designer, then how can this be achived?
0 Helpfuls
Gagandeep2023-10-11 23:31:46
Hi, We can achieve the above task by using flow designer by using approval action in it and setting the Wait For Condition of 3 days for Manager to approve it and then in case of not approved it will be assigned to Manger's Manager
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
Riya2023-03-29 05:51:21
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?
Anubhav2023-03-29 06:30:36
Sometimes, interviewer expects deeper understanding from candidates if candidate is having more experience. May be you could have explained how these things work in backend at DOM level.
e.g. We have equivalent concept in CSS, if we use visibility:hidden in CSS to hide fields, it causes their space to be retained. But, on the other hand, if we use display:none, it causes space to be removed. So, I believe this is what ServiceNow uses in backend. This answer could have impressed interviewer.
0 Helpfuls
Riya2023-03-30 05:17:07
I never knew this, it's really impressive answer.
Thank you.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
vardha2023-03-29 04:52:54
what are assignment rules?
Mohit Singhal2023-04-01 05:36:44
This module appears under the ‘System Policy application’ menu. It helps to automatically assign the tasks to a particular user or a particular group using the assigned_to and assignment_group fields respectively, depending on the specified set of conditions. One can define these assignment rules readily for their desired table.
0 Helpfuls
Kumar2023-08-15 22:22:52
Any examples?
0 Helpfuls
Sandeep2023-08-24 06:18:57
let's say we select category as 'Hardware' and subcategory as 'Mobile' based on these selections we can make 'assignment group' and 'assigned to' fields automatically display.
0 Helpfuls
Nikhil Kamlekar2024-04-01 14:30:32
Assignment Rules: In HRSD, i have used this I gave condition like if hr service is benefits inquiry and assigned to empty then you can assign to particular group.
0 Helpfuls
0 Helpfuls
*Name is mandatory
*Please enter reply
*Name is mandatory
*Please enter question
Comments
venu2024-07-03 23:51:47
thank you team
Babu Chokka2024-04-10 01:21:14
Very useful portal where we can share and gain the knowledge
saini2023-11-02 13:19:43
good work
Gunjan2023-08-23 11:29:39
Thank you for providing such quality content. This is really useful for interview preparation. I would like to give small suggestion, please add upvoting question or answer options so that reader will understand importance of question or answers.
Prashanth2023-04-13 07:06:55
Thank you so much Team. I have been asked almost all questions from your set. Finally I got offer today so thought to appreciate your work.
anonymous2023-04-12 00:58:57
Amazing content, almost in all the interviews I get these similar questions.
vardha2023-03-29 04:54:15
good content . helped me crack level 1 interview easily. keep up the good work!
SN Buddy Team2024-05-28 06:52:23
Thank you everyone for your valuable feedback, it makes us happy to see that our content is playing such crucial role in your interview preparation.