interview

 


What is GlideRecord?

GlideRecord is a JavaScript API used in ServiceNow to interact with tables in the database—like reading, creating, updating, or deleting records.


🪜 Basic GlideRecord Steps (With Explanation and Examples)

Step No.StepPurposeExample Code
1️⃣Create a GlideRecord ObjectInitialize GlideRecord on a table.var gr = new GlideRecord('incident');
2️⃣Add Conditions (Filters)Filter the records like in a query.gr.addQuery('priority', 1);
gr.addQuery('state', 'New');
3️⃣Query the TableRun the query to fetch records.gr.query();
4️⃣Loop Through ResultsIterate through each matched record.while(gr.next()) {
gs.info(gr.number);
}
5️⃣Read or Manipulate DataGet or update field values.gr.short_description = "Updated by script";
6️⃣Update or Insert RecordSave changes to the database.gr.update();
gr.insert();
7️⃣Delete (optional)Delete a record if needed.gr.deleteRecord();

Example: Update All High Priority Incidents

javascript
var gr = new GlideRecord('incident'); gr.addQuery('priority', 1); // High priority gr.query(); while (gr.next()) { gr.short_description = "This was updated via GlideRecord"; gr.update(); // Save change }

✅ Common GlideRecord Methods

MethodUse
addQuery(field, value)Add a query filter
addEncodedQuery(query)Add a complex encoded query
get(record_sys_id)Get a single record by sys_id
next()Move to the next record in the result
insert()Create a new record
update()Save changes to current record
deleteRecord()Delete the current record

🧠 Pro Tip:

  • Use GlideRecord scripts in Background Scripts, Business Rules, or Script Includes.

  • Always test in a development instance before running on production.


Would you like:

  • 🔁 GlideRecord for insert/update/delete?

  • 🧪 GlideRecord in async scripts (like GlideAjax)?

  • 📜 GlideRecord with JOIN (addJoin)?

Let me know and I’ll provide more examples for that use case.

You said:
i mean what should be written in 1 line second line third line like steps
ChatGPT said:

Ah, got it! You're asking for a step-by-step GlideRecord script format, like what exactly to write on each line — a beginner script structure.

Here’s a simple line-by-line structure you can follow when writing a basic GlideRecord script:


✅ GlideRecord Script Format (Line by Line)

javascript
// 1️⃣ Line 1: Initialize GlideRecord with table name var gr = new GlideRecord('incident'); // 2️⃣ Line 2: Add query condition(s) to filter records gr.addQuery('priority', 1); // Example: Priority = 1 // 3️⃣ Line 3: Execute the query to fetch matching records gr.query(); // 4️⃣ Line 4: Start a loop to go through each record while (gr.next()) { // 5️⃣ Line 5: Read or update field(s) inside the loop gr.short_description = 'Updated via script'; // 6️⃣ Line 6: Save the updated record back to the database gr.update(); }

✅ Explanation of Each Line

LineCodeWhat it Does
1var gr = new GlideRecord('incident');Access the "incident" table
2gr.addQuery('priority', 1);Filter: Get only high-priority incidents
3gr.query();Run the query
4while (gr.next()) {Start looping through results
5gr.short_description = 'Updated via script';Modify a field in each record
6gr.update();Save the changes
7}End of loop

💡 You can copy-paste this and just change:

  • The table name ('incident')

  • The conditions ('priority', 1)

  • The field to update (short_description)

Would you like a version for:

  • Creating a new record?

  • Deleting records?

  • Using addEncodedQuery()?

Let me know and I’ll send those line-by-line too.

You said:
giveme 10 questions answers for interview
ChatGPT said:

Here are 10 common ServiceNow GlideRecord interview questions with answers, especially useful for beginner to intermediate-level ServiceNow developer roles:


1. What is GlideRecord in ServiceNow?

Answer:
GlideRecord is a JavaScript class in ServiceNow used to interact with the database. It allows us to perform CRUD operations (Create, Read, Update, Delete) on ServiceNow tables using server-side scripts.


2. How do you retrieve all records from a table using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.query(); while(gr.next()) { gs.info(gr.number); }

This code fetches and logs the number of each record in the incident table.


3. How do you filter records in GlideRecord?

Answer:
Use .addQuery():

javascript
var gr = new GlideRecord('incident'); gr.addQuery('priority', 1); gr.query();

This retrieves incidents with priority = 1.


4. What’s the difference between .addQuery() and .addEncodedQuery()?

Answer:

  • addQuery() is used to add individual filter conditions.

  • addEncodedQuery() takes a single string with complex or multiple conditions (like you see in filter navigator).

Example:

javascript
gr.addEncodedQuery('priority=1^active=true');

5. How do you insert a new record using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.initialize(); gr.short_description = "Test Incident"; gr.priority = 2; gr.insert();

6. How do you update a specific record using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); if (gr.get('sys_id_here')) { gr.short_description = 'Updated description'; gr.update(); }

7. How do you delete a record using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); if (gr.get('sys_id_here')) { gr.deleteRecord(); }

8. What is the use of next() in GlideRecord?

Answer:
next() is used to iterate through multiple records fetched by a query. It moves the pointer to the next record.


9. How do you check if any record exists using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.addQuery('priority', 1); gr.query(); if (gr.hasNext()) { gs.info("At least one record exists."); }

10. How do you retrieve a single record using sys_id?

Answer:

javascript
var gr = new GlideRecord('incident'); if (gr.get('sys_id_value')) { gs.info(gr.short_description); }

Would you like:

  • 10 advanced GlideRecord questions?

  • 10 questions for client-side scripting?

  • Or more topics like Business Rules, Script Includes, etc.?

You said:
yes
ChatGPT said:

Great! Here's a set of 10 advanced GlideRecord interview questions with answers, especially helpful for mid-level to experienced ServiceNow developers:


10 Advanced GlideRecord Interview Questions and Answers


1. How do you perform a GlideRecord query using multiple conditions in a single line?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.addQuery('priority', 1).addQuery('state', 'New'); gr.query(); while(gr.next()) { gs.info(gr.number); }

You can chain addQuery() calls to add multiple conditions in one line.


2. How do you use addOrCondition() in GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); var qc = gr.addQuery('priority', 1); qc.addOrCondition('state', 'New'); gr.query();

This fetches records where priority = 1 OR state = New.


3. What is the use of initialize() in GlideRecord?

Answer:
.initialize() prepares a new empty record for a table so that you can set values and insert it.

javascript
var gr = new GlideRecord('incident'); gr.initialize(); gr.short_description = 'New incident'; gr.insert();

4. How do you get the number of rows returned by a GlideRecord query?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.query(); var count = 0; while(gr.next()) { count++; } gs.info("Number of records: " + count);

Or use .getRowCount() after .query() (not always accurate in all scopes).


5. What is the difference between get() and query() in GlideRecord?

Answer:

MethodPurpose
get(sys_id)Retrieves a single record using sys_id or unique value
query()Retrieves multiple records based on added conditions

6. How do you update multiple records in one go?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.addQuery('priority', 2); gr.query(); while(gr.next()) { gr.state = 3; gr.update(); }

This updates all incidents with priority = 2 and sets their state = 3.


7. How do you use GlideAggregate to count records by a specific field?

Answer:

javascript
var ga = new GlideAggregate('incident'); ga.addAggregate('COUNT'); ga.groupBy('priority'); ga.query(); while(ga.next()) { gs.info("Priority " + ga.priority + ": " + ga.getAggregate('COUNT')); }

GlideAggregate is a powerful class used for grouped counts and sums.


8. How do you join tables using GlideRecord?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.addJoinQuery('sys_user', 'assigned_to', 'sys_id'); gr.query(); while(gr.next()) { gs.info(gr.number); }

Joins can be used for combining data from related tables.


9. How do you log all fields and values of a record?

Answer:

javascript
var gr = new GlideRecord('incident'); gr.query(); if(gr.next()) { var fields = gr.getFields(); for (var i = 0; i < fields.size(); i++) { var field = fields.get(i); gs.info(field.getName() + ": " + gr.getValue(field.getName())); } }

10. How do you use GlideRecord in a Script Include and return data?

Answer:

javascript
var GetIncidents = Class.create(); GetIncidents.prototype = { initialize: function() {}, getHighPriority: function() { var gr = new GlideRecord('incident'); gr.addQuery('priority', 1); gr.query(); var result = []; while(gr.next()) { result.push(gr.getValue('number')); } return result; }, type: 'GetIncidents' };

Would you like the next set of:

  • 🔄 Client-side scripting (g_form, g_user, etc.) questions?

  • 📚 Business Rules / Script Includes / Flow Designer questions?

  • 🧠 A full mock interview for ServiceNow developer?

Let me know and I’ll tailor it accordingly!

You said:
yes
ChatGPT said:

Perfect! Here's a set of 10 Client-Side Scripting Interview Questions with Answers (focused on g_form, g_user, and related client-side APIs), followed by an optional mock interview structure suggestion:


✅ 10 Client-Side Scripting Questions & Answers for ServiceNow


1. What is g_form in ServiceNow?

Answer:
g_form is a global client-side JavaScript object used to manipulate form fields on a ServiceNow form. It is available in client scripts (onLoad, onChange, onSubmit).


2. How do you make a field read-only using g_form?

Answer:

javascript
g_form.setReadOnly('short_description', true);

This makes the short_description field read-only on the form.


3. How do you hide a field on the form?

Answer:

javascript
g_form.setDisplay('priority', false);

This hides the priority field.


4. How do you set a value on a field using g_form?

Answer:

javascript
g_form.setValue('category', 'software');

Sets the category field to “software”.


5. How do you get the current value of a field?

Answer:

javascript
var val = g_form.getValue('state');

Gets the value (not label) of the state field.


6. What is g_user in ServiceNow?

Answer:
g_user is a client-side object that provides access to information about the currently logged-in user (only in UI Policies & Catalog Client Scripts).

Example:

javascript
g_user.userID // gets sys_id g_user.hasRole('admin') // true/false

7. How do you show a mandatory field dynamically using g_form?

Answer:

javascript
g_form.setMandatory('assignment_group', true);

8. When is an onLoad client script used?

Answer:
It runs when a form loads, and is used to prepopulate fields, hide/show fields, or apply conditions before the user interacts.


9. How do you prevent a form from being submitted in onSubmit?

Answer:

javascript
function onSubmit() { if (g_form.getValue('short_description') == '') { alert('Short description is required!'); return false; // Stop form submission } return true; }

10. What is the difference between getValue() and getDisplayValue()?

Answer:

MethodReturns
getValue()The sys_id or internal value
getDisplayValue()The user-facing label or display value

💼 Optional: Mock Interview Structure for ServiceNow Developer (20–30 mins)

SectionTopicDuration
1️⃣ Intro & ExperienceYour background and ServiceNow modules used5 mins
2️⃣ GlideRecordRead, Update, Insert, Delete, Aggregates5–7 mins
3️⃣ Client Scriptsg_form, g_user, onLoad, onSubmit5–7 mins
4️⃣ Business Rules / Script IncludesUse cases and syntax5 mins
5️⃣ Real-Time Scenario“Update all open incidents for a user” type task5 mins
6️⃣ Questions for InterviewerAsk about projects, ServiceNow scope, etc.2–3 mins

Would you like:

  • 10 Business Rule & Script Include questions next?

  • A full real-time coding test scenario?

  • A ServiceNow project assignment task for practice?

Let me know and I’ll prepare it!

You said:
yes
ChatGPT said:

Excellent! Here's a set of 10 interview questions with answers focused on Business Rules and Script Includes, followed by a real-time coding task scenario you can practice.


✅ 10 ServiceNow Business Rule & Script Include Interview Questions with Answers


1. What is a Business Rule in ServiceNow?

Answer:
A Business Rule is a server-side script that runs when a record is inserted, updated, deleted, or queried. It’s used to enforce business logic on the server side.


2. What are the types of Business Rules?

Answer:

  • Before: Runs before the record is saved to the database.

  • After: Runs after the record is saved.

  • Async: Runs after the record is saved but asynchronously.

  • Display: Runs before the form is loaded (for passing data to the client).


3. How do you prevent a record from being inserted in a Business Rule?

Answer:
In a Before Insert rule:

javascript
if (current.short_description == '') { gs.addErrorMessage('Short description is required.'); current.setAbortAction(true); }

4. What is the use of current and previous in Business Rules?

Answer:

ObjectDescription
currentRepresents the current state of the record being processed.
previousRepresents the state before the current update (only on update).

5. What is a Script Include in ServiceNow?

Answer:
A Script Include is a reusable piece of server-side JavaScript logic that can be called from Business Rules, Workflows, or even client-side (via GlideAjax).


6. How do you call a Script Include from a Business Rule?

Answer:

javascript
var obj = new MyScriptInclude(); obj.myFunction();

7. How do you make a Script Include callable from client-side code?

Answer:
Set the Client Callable checkbox to true, and define your class like this:

javascript
var MyScriptInclude = Class.create(); MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, { myFunction: function() { return 'Hello from server!'; } });

8. What’s the difference between isValidRecord() and isNewRecord()?

Answer:

MethodPurpose
isNewRecord()Returns true if the GlideRecord is new.
isValidRecord()Returns true if the GlideRecord has a valid record from the database.

9. What is AbstractAjaxProcessor used for?

Answer:
It is a base class used in Script Includes that allows it to handle AJAX calls from the client side.


10. How do you call a Script Include using GlideAjax from the client side?

Answer:
Client Script:

javascript
var ga = new GlideAjax('MyScriptInclude'); ga.addParam('sysparm_name', 'myFunction'); ga.getXMLAnswer(function(response) { alert(response); });

Script Include:

javascript
var MyScriptInclude = Class.create(); MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, { myFunction: function() { return "Returned from server"; } });

🧠 Real-Time Coding Scenario Task (Assignment)

🔹 Task: Write a Before Update Business Rule on the incident table to:

  • Set the state to “On Hold”

  • Set the hold reason to “Awaiting User Info”

  • Only when the category is changed to “Inquiry / Help”


✅ Expected Code (Business Rule: Before Update → condition: current.category.changesTo("inquiry")):

javascript
if (current.category.changesTo("inquiry")) { current.state = 3; // On Hold current.hold_reason = "Awaiting User Info"; }

Would you like:

  • 📘 A hands-on mini project task to simulate real-world implementation?

  • ✍️ A set of 10 Flow Designer or Integration questions?

  • 🧪 Mock test paper for ServiceNow Developer?

Let me know and I’ll prepare it for you right away!

You said:
yes
ChatGPT said:

Awesome! Here's a complete hands-on mini project task, plus 10 Flow Designer & Integration interview questions with answers, and a mock ServiceNow Developer test paper to practice.


✅ Mini Project Task: Hands-On Simulation (Intermediate Level)

🏗️ Scenario: Auto-Assign Incident Based on Category

Goal:
When an incident is created:

  • If category = "Network", assign it to group "Network Support".

  • If category = "Database", assign it to group "DB Support".

  • Automatically set priority = 2 if it's High or Network category.


🛠️ Steps to Implement:

  1. Create a Business Rule

    • Table: incident

    • When: Before Insert

    • Conditions: Leave blank (use script)

    • Script:

javascript
if (current.category == 'network') { current.assignment_group.setDisplayValue('Network Support'); current.priority = 2; } if (current.category == 'database') { current.assignment_group.setDisplayValue('DB Support'); } if (current.impact == 1 || current.category == 'network') { current.priority = 2; }
  1. Test Cases:

    • Submit new incidents with various categories and verify if assignment group & priority are set as expected.


✅ 10 Flow Designer & Integration Questions with Answers


1. What is Flow Designer in ServiceNow?

Answer:
Flow Designer is a low-code/no-code tool that allows users to automate processes using flows, actions, and conditions without scripting.


2. What is a Trigger in Flow Designer?

Answer:
A trigger defines when the flow should run. It can be a table event (e.g., record created/updated) or a schedule.


3. How do you call a subflow from a main flow?

Answer:
Use the "Subflow" action within your main flow to call another flow.


4. What’s the difference between Flow and Subflow?

FlowSubflow
Has triggerNo trigger
Runs independentlyRuns when called from another flow

5. How do you test a flow without real data?

Answer:
Use the Test button in Flow Designer and provide sample inputs if required.


6. What is IntegrationHub in ServiceNow?

Answer:
IntegrationHub allows flows to connect with external systems using Spokes, REST, SOAP, Email, or PowerShell integrations.


7. What is a Spoke in IntegrationHub?

Answer:
A Spoke is a prebuilt set of actions, subflows, and templates for integrating with a third-party system (e.g., Slack, Microsoft Teams, Jira).


8. How do you create a custom REST integration?

Answer:

  • Navigate to: System Web Services → REST Message

  • Define Endpoint, Method (GET/POST), Headers, etc.


9. How can you log the flow execution result?

Answer:
Use the "Log" action in Flow Designer, or use system logs (e.g., in Script step via gs.log() in server script).


10. What are common authentication methods used in REST integrations?

Answer:

  • Basic Auth (username/password)

  • OAuth 2.0

  • JWT (JSON Web Token)

  • API Key


✅ Bonus: Mock ServiceNow Developer Test (20 Questions – Mix of Script, Logic, Concept)

#QuestionType
1What is the difference between before and after Business Rule?Theory
2Write a GlideRecord to fetch open incidents assigned to a groupScript
3How do you make a field hidden in a client script?Script
4What is the use of Display Business Rule?Concept
5Create a Script Include to return user departmentScript
6What is the difference between g_form.setValue() and g_form.getValue()?Theory
7Explain Script Include vs Business RuleConcept
8What is a scoped application?Concept
9Call a Script Include from GlideAjaxScript
10Explain the use of Flow DesignerConcept
11What is a Catalog Client Script?Concept
12How to set default value for a field via UI Policy?Concept
13Difference between GlideAjax and GlideRecordConcept
14Can you write GlideRecord inside a UI Policy?Trick Question
15Explain gs.info() and gs.addErrorMessage()Script
16How do you secure Script Includes from unauthorized calls?Concept
17Write code to insert a new task recordScript
18What are ACLs?Concept
19How to debug Business Rules?Concept
20What does current.update() do in After Business Rule?Trick Question

Comments

Popular posts from this blog

Non Coding IT Roles

INTEGRATION