ServiceNow Scripting Scenario Based Interview Questions 2025

Tip : For scripting related scenario based questions, usually interviewer asks you to login and share your PDI where you will have to write script. This way interviewer tries to understand your scripting skills.

Make sure you follow ServiceNow best practices while writing script.
e.g.
Do not use 'gr' variable name.
Give meaningful names to variables you declare.
Do not use multiple addQuery for single gliderecord instead try single 'addEncodedQuery'.

Write a script to show incident count for each state which are updated today?

We can use GlideAggregate API to achieve this scenario:

Script :

gs.print('State\\tIncident Count');
var grIncident=new GlideAggregate('incident');
grIncident.addEncodedQuery(' sys_updated_onONToday@javascript: gs.beginningOfToday()@javascript: gs.endOfToday()');
grIncident.addAggregate('COUNT','state');
grIncident.query();
while(grIncident.next()){
gs.print(grIncident.state. getDisplayValue()+'\t'+grIncident. getAggregate('COUNT','state'));
}

Output :


Note: This scripting question is common. However, they might ask for different table or different field. The primary learning here is whenever there is question related to finding count of the records, we are supposed to use GlideAggregate API. Do not use GlideRecord API with getRowCount method, it is not considered as best practice. Also make sure you understand how addAggregate, getAggregate and groupBy works in GlideAggregate API.

Write a script to print last 5 incidents created yesterday?

Script :


Output :


Note: Many of you must already be knowing how GlideRecord works but here interviewer checks if you know how setLimit, orderBy or orderByDesc can be used for such scenarios.

Write a script to print number of incidents Category and Sub Category wise?

We need to use GlideAggregate as we need to get count of incidents. Since, we need incidents Category and sub cateory wise, we need to apply grouping on 2 fields as shown below :

Script :


Output :


Note: Take away from this scenario is to understand how grouping on multiple fields can be used. Interviewer might change question to include 3-4 grouping fields so you should understand how grouping on multiple fields can be done via GlideAggregate API. Such multiple grouping logic is also quite useful if you want to do analysis on existing data.

Write a script as per below requirement :

1. You have to create one script inlcude with two function getIncidentDetails and sendDataToBackgroundScript.

2. getIncidentDetails should fetch first incident resolved today, this function should send Incident number, assignee user, resolved date and created date to sendDatToBackgroundScript function which is in the same script include.

3. sendDataToBackgroundScript function should calculate duration between created date and resolved date and return difference and Incident Number, Assignee User.

4. sendDataToBackgroundScript shoudl be called from background script and all data should be printed.

Background Script :


Script Include :


Output :


Note: Here interviewer tries to test your basic scripting knowledge, like calling script include from another script, sending multiple values from one function to another, finding date difference, basic usage of GlideRecord with setLimit and orderBy method. Interviewer might change scenarios to introduce more ServiceNow API's so prepare yourself with these all basic scripting knowledge.

Allow user to generate "Security Incident" via normal Incident record but we have to make sure that logged in user has access to "Security Incident" Table?

There are two methods to verify users access for any glide record.

1. Use GlideRecordSecure API while creating security incident. This API implicitly verifies if user passes ACL access on table.
2. Use canRead or canWrite function. These functions implicitly verifies if logged in user satisfies ACL access.


Prepared and confident for your interview?

Practice makes perfect! Test your skills with our virtual interview practice buddy and ensure you're fully ready for your upcoming interview.

🎯 Start Practicing

Real Interview Questions & Answers

Questions shared by ServiceNow professionals and reviewed for clarity, relevance, and interview usefulness.

Apurva 2025-10-15 07:07:12

Given an array, find the duplicates of in the array and return an object with duplicate number and its index input = [1,2,3,1] output = { 1 : [1,4]}


keerthi KIran M 2025-11-13 21:03:18
var arr= [1,2,3,1]; var duplicate = {}; for(var i=0; i< arr.length; i ){ if(duplicate[arr[i]]){ duplicate[arr[i]].push(i); }else{ duplicate[arr[i]]=[i]; } } var result = {}; for(var key in duplicate){ if(duplicate[key].length > 1){ result[key] = duplicate[key]; }} console.log(result); //output: { '1': [ 0, 3 ] }
0 Helpfuls
0 Helpfuls


Apurva 2025-10-15 07:06:43

Using a ServiceNow code, find your age.


Sowmya 2026-02-08 23:02:31
(function () { var dob = '1996-09-20'; var dobGDT = new GlideDateTime(dob); var today = new GlideDateTime(); var age = GlideDateTime.subtract(today,dobGDT).getYears(); gs.info("your age is:" age); })();
0 Helpfuls
0 Helpfuls


Apurva 2025-10-15 07:05:55

Create a table with 2 fields - Employee Name and Salary. Add 3-5 entries. Employee Name Salary Raj 2000 Keshav 3000 Seema 5000 Jason 6000 Using a ServiceNow code, Display the name of the employee with second highest salary


Durga Prasad 2026-03-24 03:58:27
var employeeList = [ { name: 'Raj', salary: 2000 }, { name: 'Keshav', salary: 3000 }, { name: 'Seema', salary: 5000 }, { name: 'Jason', salary: 6000 } ]; // Sort descending by salary employeeList.sort(function(empA, empB) { return empB.salary - empA.salary; }); // Get second highest var secondHighestEmployee = employeeList[1]; gs.print('Second highest salary employee: ' secondHighestEmployee.name);
0 Helpfuls
0 Helpfuls


Ani 2025-08-23 05:05:13

I want to create change task a b c d e and short description a1 b1 c1 d1 e1 When ever change moves to implement state How cam we achieve this scenario

0 Helpfuls


Akshay 2025-07-10 10:42:04

If Assigned To user is not active, then assign the incident to the Assigned To's manager" in ServiceNow ITSM

0 Helpfuls


Jasprit 2024-12-24 13:31:51

What is the difference between the newRecord and initialize method of GlideRecord?


swappy 2025-02-07 03:33:05
What is servicenow gliderecord initialize ? Creates an empty record within the current GlideRecord that is suitable for population before an insert. What is servicenow gliderecord newRecord? Creates a GlideRecord, sets the default values for the fields, and assigns a unique ID to the record.
0 Helpfuls
0 Helpfuls


Zorro 2024-09-29 03:12:23

Suppose there are multiple duplicates for multiple records in a incident table now I want to know all the records which have duplicates, how can I be able to achieve it?


Manoj D 2024-11-02 06:27:44
var incidentGr = new GlideAggregate('incident'); incidentGr.addAggregate('COUNT', 'number'); incidentGr.groupBy('number'); incidentGr.query(); while (incidentGr.next()) { var count = incidentGr.getAggregate('COUNT', 'number'); if (count > 1) { gs.info('Duplicate Record: ' incidentGr.number ' Count: ' count); } }
0 Helpfuls
Srinidhi 2025-08-28 18:47:05
gs.print(getDuplicates('incident','number')); // Assuming you have duplicate Incident Numbers function getDuplicates(tablename,val) { var dupRecords = []; var gaDupCheck = new GlideAggregate(tablename); gaDupCheck.addQuery('active','true'); gaDupCheck.addAggregate('COUNT',val); gaDupCheck.addNotNullQuery(val); gaDupCheck.groupBy(val); gaDupCheck.addHaving('COUNT', '>', 1); gaDupCheck.query(); while (gaDupCheck.next()) { dupRecords.push(gaDupCheck[val].toString()); } return dupRecords; }
0 Helpfuls
0 Helpfuls


Pragya 2024-06-15 01:29:28

Print user's manager's manager name and email address using client script and script include.

0 Helpfuls


Chandu 2024-05-22 12:53:13

How to pass values between two functions in Script Include


Abhinandan 2024-06-09 05:16:24
You can use function arguments to send in data from one function to other.
2 Helpfuls
0 Helpfuls


Ghouse sharief 2024-03-30 10:10:09

Populate an alert or info message on incident form when assignment group changes on the incident . on the alert display the assignment group members


Sumit 2024-10-04 11:50:13
we have to use onchange client script for displaying alert , and for fetching assignment group members we hvae to write script include which will return the list of all members in that group
0 Helpfuls
N Cinthala 2025-08-07 03:53:53
script include: var namearr=[]; var y=this.getParameter('sysparm_value'); var x=new GlideRecord('sys_user_grmember'); x.addQuery('group',y); x.query(); while(x._next()){ namearr.push(x.user.name ''); } return namearr ''; client script: var z=new GlideAjax('test123'); z.addParam('sysparm_name','demo'); z.addParam('sysparm_value',newValue); z.getXMLAnswer(hello); function hello(response){ alert(response ''); }
0 Helpfuls
0 Helpfuls


SK 2024-03-30 03:41:38

Hi, I was asked this scenario : If Assignment group and state is changed to particular choice Short description should become Mandatory and Description should become read only


Dhruv 2024-04-27 04:30:36
You can use UI Policy UI Policy condition - Assignment Group is [Service Desk] AND state is [New] UI Policy Action: Make short description field Mandatory Description filed Read Only
0 Helpfuls
0 Helpfuls


Chandu 2024-03-19 14:59:57

Difference between addQuery and addEncodedQuery methods


Nikhil Kamlekar 2024-04-01 13:01:47
addQuery or addEncodedQuery methods we usually use in querying the records. Ex: If you have one query needs to be run, you can use addQuery('category','software); If you have multiple queries then for each time you have to use addQuery instead of multiple queries you can use addEncodedQuery addEncodedQuery('paste the query here')
0 Helpfuls
0 Helpfuls


Sneha 2024-03-08 10:30:39

If I create ITIL user it will automatically added in manager group how can I do this?


Ajay 2024-10-08 10:52:31
There is a table called "sys_user_has_role" , write a Before BR on this table on insert, with the condition as role is ITIL and in the code you can include as below var user_sys_id=current.user; // this will fetch the user with that got assigned the itil role var gr=new gliderecord('sys_user_gr_member'); gr.initialize(); gr.group="Manager group sysid"; gr.user=user_sysid; gr.insert();
0 Helpfuls
0 Helpfuls


Cookie 2024-03-06 08:50:36

If an incident is in resolved state for 5 days, Close it


Anjali Singh 2024-03-25 21:00:09
1. Navigate to System Properties > System. 2. Locate the property Number of days (integer) after which Resolved incidents are automatically closed. 3. Set the Value to be the number of days to wait before closing a resolved incident.
0 Helpfuls
0 Helpfuls


Cookie 2024-03-06 08:49:56

Print system date and date a year ago


Pradeep 2024-04-15 04:23:07
var gdt = new GlideDateTime(); gdt.addYears(-1); gs.print(gdt.getDate());
0 Helpfuls
0 Helpfuls


Cookie 2024-03-06 08:48:55

Insert 10 incidents from Incident table in an array


Heisenberg 2024-12-24 13:08:57
var incident_list=[]; var incidentGr=new GlideRecord('incident'); incidentGr.addEncodedQuery('active=true'); //Replace query based on the input incidenGr.setLimit(10); incidentGr.query(); while(incidentGr.nect()){ var obj={}; //Empty object //Push attributes according to your choice. For now I am pushing only number and short description obj.number=incidentGr.getValue('number'); obj.short_description=incidentGr.getValue('short_description'); incident_list.push(obj); } //Print List for(var i=0;i
0 Helpfuls
Divesh Jalandriya 2025-05-22 03:27:11
var arrayList = []; var grIncident = new GlideRecord('incident'); grIncident.setLimit(10); grIncident.query(); while (grIncident.next()) { var obj = {}; obj.number = grIncident.number.getDisplayValue(); obj.short_description = grIncident.short_description; arrayList.push(obj.number '->' obj.short_description); } for(var i=0;i
0 Helpfuls
0 Helpfuls


Cookie 2024-03-06 08:48:38

Read Caller's name from the Caller field in incident table, and check for 1st letter, if it is a, print "apple", if it is b, print "bat"


Pradeep 2024-04-21 07:55:08
var caller = current.caller_id.getDisplayValue(); var text = caller.toUpperCase(); if (text.startsWith('A')) gs.print('Apple'); else gs.print('Bat');
0 Helpfuls
0 Helpfuls


Naveen 2024-03-01 09:08:46

2.When the Caller field is changed, retrieve the caller's location and update the Location field accordingly.


Ajay 2024-10-08 11:59:33
you can achieve this with a on change client script and script include
0 Helpfuls
0 Helpfuls


sujatha 2024-02-28 04:17:04

when ever the problem work note updated,update the comment in attached incident work notes also


Nithin 2025-07-03 23:54:19
script in problem table >> var inc = new GlideRecord('incident'); inc.addQuery('problem_id', current.sys_id); inc.query(); while (inc.next()) { inc.work_notes = current.work_notes; inc.update(); }
0 Helpfuls
0 Helpfuls


ann 2023-10-25 22:22:52

when I submit a new request on behalf of "xyz" when xyz opens that form should be editable for "xyz" and other forms should be read only to xyz how can I achieve this.


abdul 2023-11-16 03:22:55
created_by field will store your name and requested_by field will store 'XYZ'. NOw you can USE ACL or combination of Display BR and Client script
1 Helpfuls
Pratiksha 2023-11-24 06:53:23
suppose you create incident on behalf of xyz the you can achieve above requirement in two ways. 1.create write ACL (in script check if logged in user is caller) 2. create display BR and onLoad client script
0 Helpfuls
ISHA 2023-11-28 11:33:26
ACL
0 Helpfuls
Astik Thombare 2023-12-04 07:14:47
Hi @Ann, You can create a new ACL on the incident table with the operation "write" and name it "Incident." Set the conditions to "None" and give the condition that the caller is dynamic. This way, you can achieve the above requirement. Thanks! Astik
2 Helpfuls
satyam jain 2024-03-02 06:10:06
Create write ACL on the table.none and write the script in advance answer=false; var gr=gs.getUserID(); if(current.caller_id==gr){ answer=true; }
0 Helpfuls
0 Helpfuls


ann 2023-10-25 22:18:40

when user clicks on new form incident form will open, when user opens a new option that time in the state "cancel" option should be hidden. Once he submits a form "cancel" option should be visible. how to achieve this


snowexpertaastik 2025-09-05 09:59:55
if(g_form.isNewRecord()){ g_form.removeOption('state','8'); }
0 Helpfuls
0 Helpfuls


ann 2023-10-25 22:13:54

on the catalog form below the submit button i want to add reset button which will reset all the values. How to achieve this

0 Helpfuls


Harish Gadi 2023-09-07 05:04:12

How to create a problem record for 5 th incident in incident list Example: there are 10 incidents we need to create a problem ticket for incident for 5 th incident only ?


Surendra 2023-10-24 22:31:11
var gr = new GlideRecord('incident'); gr.orderByDesc('sys_created_on'); //gr.setLimit(10); gr.chooseWindow(4,5);// 4-first row and 5- last row gr.query(); while(gr.next()){ gs.print(gr.number); // create problem record here }
0 Helpfuls
0 Helpfuls


Satyapriya Biswal 2023-08-05 04:58:56

I was asked that write a background script that will show how many roles the current logged in user have. Can anyone help me to answer this question.


Aastha 2023-08-06 13:11:44
var gr = new GlideRecord('sys_user_has_role'); gr.addQuery('user','6816f79cc0a8016401c5a33be04be441') gr.query(); gs.print(gr.getRowCount()); while(gr.next()) { //one way to do it }
0 Helpfuls
ps 2023-08-07 08:56:19
var userid = gs.getUserID(); var gr = new GlideRecord('sys_user_has_role'); gr.addQuery('user.sys_id', userid); gr.query(); while(gr.next()){ gs.print(gr.role.getDisplayValue()); gs.print(gr.getRowCount()); }
0 Helpfuls
Ria 2023-08-23 11:15:54
Whenever we have to find out count of something then it is recommended to use GlideAggregate API, using GlideRecord API with getRowCount method is not efficient and it is not considered as best practice. Therefore you can rather use below code. var userid = gs.getUserID(); var gaRoleCount=new GlideAggregate("sys_user_has_role"); gaRoleCount.addQuery("user",userid); gaRoleCount.addAggregate("COUNT","role"); gaRoleCount.query(); if(gaRoleCount.next()){ gs.print("Role Count for logged in user:" gaRoleCount.getAggregate("COUNT","role"); }
1 Helpfuls
Satyapriya Biswal 2023-08-25 02:11:26
Thanks
0 Helpfuls
Praveen K 2023-09-20 23:43:56
Hi Ria, In 4th line you are grouping the count by role which is incorrect it should be only gaRoleCount.addAggregate("COUNT"); Check below code for more info var userid = gs.getUserID(); var gaRoleCount=new GlideAggregate("sys_user_has_role"); gaRoleCount.addQuery("user",userid); gaRoleCount.addAggregate("COUNT"); gaRoleCount.query(); if(gaRoleCount.next()){ //gs.print(gs.getUserName()) //gs.print(userid) gs.print("Role Count for logged in user:" gaRoleCount.getAggregate("COUNT")); }
1 Helpfuls
Bharath 2024-07-12 13:27:33
gs.print("Role Count for logged in user:" gaRoleCount.getAggregate("COUNT")) in this line was error in background script, Please once check it Tank You..
0 Helpfuls
anonymous 2026-02-23 04:38:52
var a=gs.getUser().getRoles(); gs.log(a); gs.log(a.size());
0 Helpfuls
0 Helpfuls


Swetha 2023-07-16 11:33:42

During my interview, I was asked to write a script where I need to return a user's Manager's Manager(Seniormanager). If the field is empty, in that case i have to return his above level manager (Director). Can you help me with the solution code.


Praveen K 2023-09-20 23:58:17
Try using Dot walking and if statement for example - var gr = new GlideRecord('sys_user') gr.addQuery('sys_id','02826bf03710200044e0bfc8bcbe5d88'); gr.query(); if (gr.next()){ gs.print('User Name - ' gr.name) gs.print('Manager Name - ' gr.manager.name) if(gr.manager.manager.name){ gs.print('Managers Manager Name - ' gr.manager.manager.name) //Make sure that managers field is populated in users and managers record } else{ gs.print('Director Details (I dont have director so not displaying)') } }
0 Helpfuls
0 Helpfuls


Mohit Singhal 2023-04-01 04:23:11

Write Background script code to Make all the incidents active as false whose caller starts with 's'.


Kushal 2023-04-03 21:53:38
There is small trick in this scenario. Many developer don't know that we can dot walk caller name in encoded query itself (2nd line in below script). Instead developers glide into all incidents and then they add if condition to check if caller name startswith s, which is valid solution but not efficient one. Script :var grIncident=new GlideRecord("incident"); grIncident.addEncodedQuery("caller_id.nameSTARTSWITHs"); //Here dot walk is very important, very useful in many scenarios grIncident.query(); while(grIncident.next()){ grIncident.setValue("active",false); grIncident.update(); }
1 Helpfuls
Prerna 2023-04-03 23:33:21
Thanks Kushal. I never knew dot walk work this way as well.
1 Helpfuls
Rakesh 2023-04-06 05:45:08
Hey Kushal, Your script works but it is not efficient. In this case we can rather use updateMultiple method as we are going to update same value for all filtered records ( that is making it false ). Interviewer normally expects efficient script from candidates. We can use below script : var grIncident=new GlideRecord("incident"); grIncident.addEncodedQuery("caller_id.nameSTARTSWITHs"); grIncident.query(); grIncident.setValue("active",false); grIncident.updateMultiple();
1 Helpfuls
Mohit Singhal 2023-05-01 11:49:12
As I have read , Incident's Active is set to false when Incident state is Closed or Cancelled.
1 Helpfuls
Astik 2023-12-04 07:43:26
var gr = new GlideRecord('incident'); gr.addEncodedQuery('caller_idSTARTSWITHs^ORcaller_idSTARTSWITHWITHS'); gr.query(); while(gr.next()){ gr.setValue('active',false); gr.update(); }
0 Helpfuls
Ashutosh Kumar 2024-10-20 22:38:17
you need to change state of new cases to canceled/closed/resolve than only you can set Active false. var gr = new GlideRecord('incident'); gr.addEncodedQuery('caller_idSTARTSWITHs^active=true'); gr.query(); while(gr.next()){ gr.state=8; gr.active=false; gr.update(); }
0 Helpfuls
0 Helpfuls








🚀 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.

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:

UPI QR Code

Comments

Sonali 2025-01-31 06:45:25
This is a brilliant set of questions and answers! I’ve been asked many of these questions repeatedly. I must also admit, I’m finding the user-shared questions incredibly interesting and valuable for my interview preparation. I am just loving the fact that everyone is contributing their questions and answers here.



Prerna 2023-04-03 22:12:30
I can't express how much your content is useful for me. Around 70% questions were asked to me are from your set of question and answers. Now I am able to crack L1 interviews easily.