If you are new to ServiceNow development, the GlideRecord script is an essential tool to master. GlideRecord scripts allow you to interact with the database and retrieve, insert, or modify records in ServiceNow. Here are the basic steps to get started with GlideRecord scripts:
Step 1: Understand the Basics Before you start writing GlideRecord scripts, it's important to have a basic understanding of JavaScript and the ServiceNow data model. You should also have access to a ServiceNow instance to practice on.
Step 2: Create a New Script To start a GlideRecord script, you first need to create a new script include or business rule. In the script, you can specify which table or tables you want to interact with.
Step 3: Define the GlideRecord Object Next, you need to define a GlideRecord object. This object represents a table in the ServiceNow database and is used to interact with the table's records.
Here are the six most common GlideRecord functions and examples of how they are used:
- addQuery() - The addQuery() function allows you to add a query condition to your GlideRecord object. You can use this function to retrieve specific records from the table.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('priority', '1');
incidentGR.query();
In this example, the GlideRecord object is created for the incident table, and the addQuery() function is used to add a condition where the priority field equals 1. The query() function is then called to execute the query and retrieve the matching records.
- addEncodedQuery() - The addEncodedQuery() function allows you to add a query encoded as a string to your GlideRecord object. This is useful when you want to construct a complex query using multiple conditions. Here's an example:
var gr = new GlideRecord('incident');
gr.addEncodedQuery('priority=1^ORpriority=2');
gr.query();
while (gr.next()) {
// do something with the records
}
In this example, the addEncodedQuery() function is used to construct a query that returns incidents with a priority of either 1 or 2. The query() function is then called to execute the query, and the while loop is used to iterate through the result set.
💡 Pro Tip: If you can use the filters on a list page in ServiceNow to filter the correct data that you want to see, you can right click on the breadcrumbs link on the top left of the page and you will get an option to "Copy query". This will copy the encoded query to your clipboard that is needed in the script above. This should help you speed up your development time so you don't have to learn what all of the symbols mean.
- getValue() - The getValue() function retrieves the value of a field in the current GlideRecord object.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
var shortDesc = incidentGR.getValue('short_description');
In this example, the GlideRecord object is created for the incident table, and the get() function is used to retrieve the record with the number INC0010001. The getValue() function is then used to retrieve the value of the short_description field for that record.
- setValue() - The setValue() function allows you to set the value of a field in the current GlideRecord object.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
incidentGR.setValue('state', '2');
incidentGR.update();
In this example, the GlideRecord object is created for the incident table, and the get() function is used to retrieve the record with the number INC0010001. The setValue() function is then used to set the state field to 2, indicating that the incident is in progress. The update() function is then called to save the changes to the record.
- insert() - The insert() function allows you to insert a new record into the current GlideRecord object.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.initialize();
incidentGR.setValue('short_description', 'New Incident');
incidentGR.insert();
In this example, the GlideRecord object is created for the incident table, and the initialize() function is used to create a new, empty record. The setValue() function is then used to set the short_description field, and the insert() function is called to save the new record to the database.
- deleteRecord() - The deleteRecord() function allows you to delete the current record from the GlideRecord object.
Example:
var incidentGR = new GlideRecord('incident');
incidentGR.get('number', 'INC0010001');
incidentGR.deleteRecord();
In this example, the GlideRecord object is created for the incident table, and we use the get() function to lookup the exact record that we want in this situation, then use the deleteRecord() function to delete this incident from the table.
These are just a few of the most common GlideRecord functions. By learning how to use these functions effectively, you can create powerful scripts that automate processes, simplify workflows, and make your ServiceNow instance more efficient.
As the demand for ServiceNow solutions continues to grow, so does the demand for ServiceNow Technical Consultants. If you're considering a career in the IT industry, becoming a ServiceNow Technical Consultant could be a great option for you. Here are some of the benefits of pursuing this career path, as well as tips on how to get started.
What is a ServiceNow Technical Consultant?
A ServiceNow Technical Consultant is responsible for designing, implementing, and maintaining ServiceNow solutions for clients. They work with clients to understand their business processes, identify areas for improvement, and develop solutions using ServiceNow technology.
Benefits of becoming a ServiceNow Technical Consultant
-
High demand: ServiceNow is one of the fastest-growing cloud-based software companies in the world, and the demand for ServiceNow Technical Consultants continues to grow. This means that there are plenty of job opportunities available for those with the right skills and experience.
-
Lucrative salary: The average salary for a ServiceNow Technical Consultant is around $100,000 per year, and it can go up to $150,000 per year or more depending on experience and expertise.
-
Variety of work: ServiceNow Technical Consultants work on a variety of projects, which keeps the job interesting and challenging. They work with clients from different industries and can work on projects ranging from small-scale implementations to large, enterprise-wide implementations.
-
Flexibility: Many ServiceNow Technical Consultants work on a contract or freelance basis, which provides them with the flexibility to choose their own hours and work from anywhere in the world.
How to get started as a ServiceNow Technical Consultant
-
Gain ServiceNow knowledge: To become a ServiceNow Technical Consultant, you need to have a strong understanding of the ServiceNow platform. You can start by taking free ServiceNow training courses available on the ServiceNow website or signing up for the ServiceNow Developer Program.
-
Get certified: ServiceNow offers several certifications that can help you demonstrate your expertise in the platform. Some popular certifications for ServiceNow Technical Consultants include Certified Implementation Specialist and Certified System Administrator.
-
Gain experience: To land a job as a ServiceNow Technical Consultant, you'll need to have some experience working with the platform. Look for internships or entry-level positions at companies that use ServiceNow or consider contributing to open source ServiceNow projects.
-
Network: Attend industry events and connect with other ServiceNow professionals on LinkedIn or other social media platforms. Building relationships with others in the industry can help you learn about job opportunities and gain valuable insights into the ServiceNow ecosystem.
In conclusion, becoming a ServiceNow Technical Consultant can be a rewarding career path with plenty of opportunities for growth and advancement. By gaining ServiceNow knowledge, getting certified, gaining experience, and networking, you can set yourself up for success in this exciting field.