Job Switch Kit: Everything you need to crack your next ServiceNow interview Get Job Switch Kit →
Get the complete ServiceNow interview prep system — 500+ Q&A, mock interviews & more Get Job Switch Kit →

ServiceNow Scripting Scenario Based Interview Questions 2026

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.

Most candidates who fail weren't underskilled.
They were underprepared.

These free questions cover the basics. But interviews go deeper. Discovery, ITOM, SecOps, Flow Designer, Now Assist, AI Agents. The Job Switch Kit gives you the full 15-day roadmap, 500+ structured Q&A across every module, a LinkedIn profile optimisation guide so recruiters find you before you even apply, and an interview bot that practices with you until you're ready. Most candidates walk in under-prepared. Don't be one of them.

500+ Q&A — Technical & Behavioral, Every Module

Battle-tested questions with structured answers across every high-frequency topic — ITOM, SecOps Vulnerability Response, Discovery, Flow Designer, AI Agents, Generative AI, and Now Assist. Plus a dedicated Behavioral round covering HR questions, STAR method answers, salary negotiation, and career direction — because technical prep alone is not enough to get the offer

15-Day Structured Roadmap

Day-by-day curriculum in the right order — scripting fundamentals, ITSM & CMDB, integrations, AI modules, and behavioral prep — so you peak exactly on interview day

Unlimited Interview Bot

Two modes: system-guided sessions with curated ServiceNow questions, and self-practice where you bring your own questions. Every session is recorded — download your Q&A transcript, review your answers, and pinpoint exactly where you need to improve

Salary Negotiation Playbook

Proven strategies for counter-offers, CTC decoding & in-hand salary strategy

Job Switch Strategy

Notice period tactics, BGV prep, resignation playbook & offer comparison

Progress Tracker + Notes

Track completion per topic, rate your confidence per question, add private notes at question level and page level. Only you see them. Always know exactly what to tackle next

LinkedIn Profile Optimisation

Step-by-step guide to rank in recruiter searches before you apply. Module-specific keyword lists, role-based headline and About section templates, experience bullet formulas, and certification display guidance built specifically for ServiceNow professionals

Get Job Switch Kit →

🔒 Secure payment via Razorpay · Instant access after payment

10 days free — for your honest review

Your story could be the reason someone else lands their next ServiceNow role

If this content made a real difference in your prep, sharing that experience with your network helps other professionals discover it — and we'd love to say thank you with 10 days of premium access, completely on us.

Real Interview Questions & Answers

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

Apurva 15 Oct 2025
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 13 Nov 2025
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 helpful
0 helpful
Apurva 15 Oct 2025
Using a ServiceNow code, find your age.
Sowmya 08 Feb 2026
(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 helpful
0 helpful
Apurva 15 Oct 2025
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 24 Mar 2026
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 helpful
0 helpful
Ani 23 Aug 2025
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 helpful
Akshay 10 Jul 2025
If Assigned To user is not active, then assign the incident to the Assigned To's manager" in ServiceNow ITSM
Surya 06 Apr 2026
what is manager is not part of the assignment group
0 helpful
0 helpful
Jasprit 24 Dec 2024
What is the difference between the newRecord and initialize method of GlideRecord?
Swappy 07 Feb 2025
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 helpful
0 helpful
Zorro 29 Sep 2024
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 02 Nov 2024
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 helpful
Srinidhi 28 Aug 2025
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 helpful
0 helpful
Pragya 15 Jun 2024
Print user's manager's manager name and email address using client script and script include.
0 helpful
Chandu 22 May 2024
How to pass values between two functions in Script Include
Abhinandan 09 Jun 2024
You can use function arguments to send in data from one function to other.
2 helpful
0 helpful
Ghouse Sharief 30 Mar 2024
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 04 Oct 2024
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 helpful
N Cinthala 07 Aug 2025
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 helpful
0 helpful
Sk 30 Mar 2024
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 27 Apr 2024
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 helpful
0 helpful
Chandu 19 Mar 2024
Difference between addQuery and addEncodedQuery methods
Nikhil Kamlekar 01 Apr 2024
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 helpful
0 helpful
Sneha 08 Mar 2024
If I create ITIL user it will automatically added in manager group how can I do this?
Ajay 08 Oct 2024
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 helpful
0 helpful
Cookie 06 Mar 2024
If an incident is in resolved state for 5 days, Close it
Anjali Singh 25 Mar 2024
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 helpful
0 helpful
Cookie 06 Mar 2024
Print system date and date a year ago
Pradeep 15 Apr 2024
var gdt = new GlideDateTime(); gdt.addYears(-1); gs.print(gdt.getDate());
0 helpful
0 helpful
Cookie 06 Mar 2024
Insert 10 incidents from Incident table in an array
Heisenberg 24 Dec 2024
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 helpful
Divesh Jalandriya 22 May 2025
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 helpful
0 helpful
Cookie 06 Mar 2024
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 21 Apr 2024
var caller = current.caller_id.getDisplayValue(); var text = caller.toUpperCase(); if (text.startsWith('A')) gs.print('Apple'); else gs.print('Bat');
0 helpful
0 helpful
Naveen 01 Mar 2024
2.When the Caller field is changed, retrieve the caller's location and update the Location field accordingly.
Ajay 08 Oct 2024
you can achieve this with a on change client script and script include
0 helpful
0 helpful
Sujatha 28 Feb 2024
when ever the problem work note updated,update the comment in attached incident work notes also
Nithin 03 Jul 2025
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 helpful
0 helpful
Ann 25 Oct 2023
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 16 Nov 2023
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 helpful
Pratiksha 24 Nov 2023
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 helpful
Isha 28 Nov 2023
ACL
0 helpful
Astik Thombare 04 Dec 2023
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 helpful
Satyam Jain 02 Mar 2024
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 helpful
0 helpful
Ann 25 Oct 2023
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 05 Sep 2025
if(g_form.isNewRecord()){ g_form.removeOption('state','8'); }
0 helpful
0 helpful
Ann 25 Oct 2023
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 helpful
Harish Gadi 07 Sep 2023
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 24 Oct 2023
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 helpful
0 helpful
Satyapriya Biswal 05 Aug 2023
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 06 Aug 2023
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 helpful
Ps 07 Aug 2023
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 helpful
Ria 23 Aug 2023
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 helpful
Satyapriya Biswal 25 Aug 2023
Thanks
0 helpful
Praveen K 20 Sep 2023
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 helpful
Bharath 12 Jul 2024
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 helpful
Anonymous 23 Feb 2026
var a=gs.getUser().getRoles(); gs.log(a); gs.log(a.size());
0 helpful
0 helpful
Swetha 16 Jul 2023
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 20 Sep 2023
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 helpful
0 helpful
Mohit Singhal 01 Apr 2023
Write Background script code to Make all the incidents active as false whose caller starts with 's'.
Kushal 03 Apr 2023
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 helpful
Prerna 03 Apr 2023
Thanks Kushal. I never knew dot walk work this way as well.
1 helpful
Rakesh 06 Apr 2023
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 helpful
Mohit Singhal 01 May 2023
As I have read , Incident's Active is set to false when Incident state is Closed or Cancelled.
1 helpful
Astik 04 Dec 2023
var gr = new GlideRecord('incident'); gr.addEncodedQuery('caller_idSTARTSWITHs^ORcaller_idSTARTSWITHWITHS'); gr.query(); while(gr.next()){ gr.setValue('active',false); gr.update(); }
0 helpful
Ashutosh Kumar 20 Oct 2024
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 helpful
0 helpful
Share a Question

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

Comments 2
S
Sonali 31 Jan 2025
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.
P
Prerna 03 Apr 2023
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.
📝 My Topic Notes 🔒 Only visible to you
Log in or sign up free to save notes
Previous Scenario Based Questions Next ACL Questions

Found these questions helpful?

Share your experience — it helps other ServiceNow professionals know what to prepare.

Share Your Story →