We are experimenting with new training content delivery methods. This tutorial blog post is one of those experiments. We are very interested in your feedback. Please let us know what you think about this format and the content in the comments below.
Introduction
Newer developers in the Developer Program have requested additional hands-on work with GlideRecord. While many of the training modules include examples with GlideRecord, this “bonus exercise” adds hands-on experience with GlideRecords in Business Rules. The topics in this post build on concepts in the Server-side Scripting module.
In this exercise, you create three Business Rules.
- Guided: Create a Business Rule for new Incidents to check for active Incidents from the same caller and post an information message with the caller’s active Incidents.
- Guided: Create a Business Rule for Configuration Items (CIs) to check for active Tasks associated with the CI when the CI is decommissioned.
- Challenge:
- Option 1: Create a similar Business Rule for new NeedIt requests.
- Option 2: Create a similar Business Rule for new Change Requests.
NOTE: This exercise uses demo data that may vary from your instance.
Create an Incident Business Rule
- Log in to your ServiceNow instance as a System Administrator.
- View Incidents where Rick Berzle is the Caller.
- Use the Application Navigator to open Incident > Open.
- Change Go to to Caller and set the search value to *Berzle. Make note of how many records have Rick Berzle as the Caller.
- Use the Application Navigator to open Incident > Open.
Create a Business Rule.
- Right-click any column header and select the Configure > Business Rules menu item.
- Click the New button.
- Configure the Business Rule.
- Name: List Open Incidents by Same Caller
- Advanced: selected (checked)
- Name: List Open Incidents by Same Caller
- Configure the When to run section.
- When: before
- Insert: selected (checked)
- When: before
Configure the Advanced section.
Replace the contents of the script field with this script. NOTE: this script contains some text you will update in the next step.
(function executeRule(current, previous /*null when async*/) { // 1. Create an object to store rows from a table var grIncident = new CLASS('incident'); // 2. Build query grIncident.QUERY_CONDITION_METHOD('active','=',true); grIncident.QUERY_CONDITION_METHOD('caller_id','=',current.caller_id); // 3. Execute query grIncident.RUN_QUERY_METHOD(); // Initialize message to list Caller's active Incidents var msg = "Caller's other active incidents: "; // 4. Process returned records while(grIncident.ITERATE_METHOD()){ // add a new line to the message for each of the Caller's active Incidents msg = msg + grIncident.getValue('number') + ": " + grIncident.getValue('short_description') + " | "; } gs.addInfoMessage(msg); })(current, previous);
Update the script with the class and methods.
- Replace CLASS with the class of the object to create.
- Replace QUERY_CONDITION_METHOD with the method to add a condition to the query.
- Replace RUN_QUERY_METHOD with the method to run the query.
- Replace ITERATE_METHOD with the method to iterate through the returned records.
QUESTION: What parameter do you pass to the CLASS? What are the parameters passed to the QUERY_CONDITION_METHOD? If you are not sure, scroll to the Answers section at the bottom of this page.
NOTE: If you need help with any of these, see the Create an Incident Business Rule section in the answers at the bottom of this post for the complete script.
- Replace CLASS with the class of the object to create.
Click the Submit button.
- Right-click any column header and select the Configure > Business Rules menu item.
Test the Business Rule.
- Create an Incident with Rick Berzle as the Caller.
- Use the Application Navigator to open Incident > Create New.
- Configure the Incident.
- Caller: Rick Berzle
- Short description: Received phishing email
- Caller: Rick Berzle
- Click the Submit button. An information message lists active Incidents with Rick Berzle as the Caller.
- Create an Incident with Rick Berzle as the Caller.
Create a Configuration Item Business Rule
When a Configuration Item (CI) is deactivated, you want to update all tasks associated with the deactivated CI that the CI has been deactivated. In this hands-on section, you create a Business Rule for the Configuration Item [cmdb_ci] table that finds all tasks associated with the CI and writes a Work note that the CI has been deactivated.
- Set up a Configuration Item with tasks to use for testing.
- Use the Application Navigator to open Incident > Open.
- Click the Personalize List
icon.
- Add the Configuration item column to the Selected list in the location of your choice.
- Sort by Configuration Item so tasks with no Configuration Item are at the top of the list.
- Open the first Incident with no Configuration item.
- Set the Configuration item value to *JEMPLOYEE-IBM.
- Click the Additional actions button and select the Create Favorite menu item to easily return to the Incident later.
- Click the Additional actions button and select the Save menu item.
- Use the Application Navigator to open Incident > Open.
Create a Business Rule for the Configuration Item table.
- Click the Preview this record button for the Configuration item.
- Click the Open Record button in the preview.
- Click the Additional actions button and select the Configure > Business Rules menu item.
- Click the New button.
- Configure the Business Rule.
- Name: Update Tasks on CI Deactivation
- Table: Configuration Item [cmdb_ci]
- Advanced: selected (checked)
- Name: Update Tasks on CI Deactivation
- Configure the When to run section.
- When: before
- Insert: selected (checked)
- Update: selected (checked)
- Filter Conditions: [Status] [Changes from] [Installed]
- When: before
Configure the Advanced section.
Replace the contents of the script field with this script. NOTE: this script contains some text you will update in the next step.
(function executeRule(current, previous /*null when async*/) { // 1. Create an object to store rows from a table var grTask = new GlideRecord('TABLE'); // 2. Build query grTask.addQuery('active','=','true'); grTask.addQuery('COMPARISON_FIELD','=',COMPARISON_VALUE); // 3. Execute query grTask.query(); // 4. Process returned records while(grTask.next()){ // Write to Work notes that CI is not Installed grTask.work_notes = current.name + " is no longer active"; grTask.update(); } })(current, previous);
Update the script with the object and methods.
- Replace TABLE with the table to query.
- Replace COMPARISON_FIELD with the field on a task record for the Configuration item.
- Replace COMPARISON_VALUE with the value from the CI record to match to the task.
NOTE: If you need help with any of these, see the Create a CI Business Rule section in the answers at the bottom of this post for the complete script.
- Replace TABLE with the table to query.
Click the Submit button.
- Click the Preview this record button for the Configuration item.
Test the Business Rule.
- Add the Status field to the Computer form.
- Find and open the Computer named *JEMPLOYEE-IBM.
- Set the Status for *JEMPLOYEE-IBM to Retired.
- From the Favorites in the Application Navigator, select the Incident previously saved to the favorites.
- Find the message *JEMPLOYEE-IBM is no longer active in the Activities.
- Add the Status field to the Computer form.
Did you do the hands-on exercise in this blog? Click here to let us know!
Challenge - Option 1 - Create a NeedIt Business Rule
Create a Business Rule for the NeedIt application from the Developer Portal training that finds and lists open NeedIt requests for the user in the Requested for field. Display the list in an information message.
Starting tips:
- Prepare the instance with the NeedIt application using the directions in Exercise: Prepare Instance for Server-side Scripting.
- Create the Business Rule for the NeedIt table.
- Use the Requested for field.
If you need help, see the Create a NeedIt Business Rule section in the answers at the bottom of this post for a sample script.
Challenge - Option 2 - Create a Change Business Rule
Create a Business Rule for Changes that find and lists other changes opened for the same Configuration Item. Dsiplay the list in an information message.
Starting tips:
- What is the name of the Change table?
- What is the name of the Configuration Item field?
If you need help, see the Create a Change Business Rule section in the answers at the bottom of this post for a sample script.
Answers
Create an Incident Business Rule: Replace the placeholders in the script with the correct object and methods.
Replace CLASS with GlideRecord
Replace QUERY_CONDITION_METHOD with addQuery
Replace RUN_QUERY_METHOD with query
Replace ITERATE_METHOD with next
If you are still stuck, review the components of a GlideRecord query in the Server-side scripting training module.The resulting script:
(function executeRule(current, previous /*null when async*/) {
// 1. Create an object to store rows from a table
var grIncident = new GlideRecord('incident');
// 2. Build query
grIncident.addQuery('active','=',true);
grIncident.addQuery('caller_id','=',current.caller_id);
// 3. Execute query
grIncident.query();
// Initialize message to list Caller's active Incidents
var msg = "Caller's other active incidents: ";
// 4. Process returned records
while(grIncident.next()){
// add a new line to the message for each of the Caller's active Incidents
msg = msg + grIncident.getValue('number') + ": " + grIncident.getValue('short_description') + " | ";
}
gs.addInfoMessage(msg);
})(current, previous);
Question: What parameter do you pass to the CLASS? What are the parameters passed to the QUERY_CONDITION_METHOD?
Answer: Pass the table on which to perform the query to the GlideRecord class. Pass the field name, operator, and value to the addQuery method.
Create a NeedIt Business Rule: Here is an example script for a Business Rule to get the list of other NeedIt requests for a user and write them to an information message.
(function executeRule(current, previous /*null when async*/) {
// 1. Create an object to store rows from a table
var grNeedIt = new GlideRecord('x_58872_needit_needit');
// 2. Build query
grNeedIt.addQuery('active','=',true);
grNeedIt.addQuery('u_requested_for','=',current.u_requested_for);
// 3. Execute query
grNeedIt.query();
// Initialize message to list NeedIt requests for user
var msg = "Other NeedIt Requests for user: ";
// 4. Process returned records
while(grNeedIt.next()){
// add a new line to the message for each NeedIt for the user
msg = msg + grNeedIt.getValue('number') + ": " + grNeedIt.getValue('short_description') + " | ";
}
gs.addInfoMessage(msg);
})(current, previous);
Create a Change Business Rule: Here is an example script for a Business Rule to get the list of other Changes for a Configuration Item and write them to an information message.
(function executeRule(current, previous /*null when async*/) {
// 1. Create an object to store rows from a table
var grChange = new GlideRecord('change_request');
// 2. Build query
grChange.addQuery('active','=',true);
grChange.addQuery('cmdb_ci','=',current.cmdb_ci);
// 3. Execute query
grChange.query();
// Initialize message to list Changes for the CI
var msg = "Other Changes Requests for CI " + current.cmdb_ci.name + ": ";
// 4. Process returned records
while(grChange.next()){
// add a new line to the message for each Change for the CI
msg = msg + grChange.getValue('number') + ": " + grChange.getValue('short_description') + " | ";
}
gs.addInfoMessage(msg);
})(current, previous);
Create a CI Business Rule: Replace the placeholders in the script with the correct table and query parameters.
Replace TABLE with task
Replace COMPARISON_FIELD with cmdb_ci Replace COMPARISON_VALUE with current.sys_idThe resulting script:
(function executeRule(current, previous /*null when async*/) {
// 1. Create an object to store rows from a table
var grTask = new GlideRecord('task');
// 2. Build query
grTask.addQuery('active','=','true');
grTask.addQuery('cmdb_ci','=',current.sys_id);
// 3. Execute query
grTask.query();
// 4. Process returned records
while(grTask.next()){
// Write to Work notes that CI is not Installed
grTask.work_notes = current.name + " is no longer active";
grTask.update();
}
})(current, previous);
Share this post
Twitter
Facebook
Reddit
LinkedIn
Email