Ben Sweetser

9 minute read

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

  1. Log in to your ServiceNow instance as a System Administrator.
  2. View Incidents where Rick Berzle is the Caller.
    1. Use the Application Navigator to open Incident > Open.
    2. Change Go to to Caller and set the search value to *Berzle. Make note of how many records have Rick Berzle as the Caller.
  3. Create a Business Rule.

    1. Right-click any column header and select the Configure > Business Rules menu item.
    2. Click the New button.
    3. Configure the Business Rule.
      1. Name: List Open Incidents by Same Caller
      2. Advanced: selected (checked)
    4. Configure the When to run section.
      1. When: before
      2. Insert: selected (checked)
    5. Configure the Advanced section.

      1. 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);
        
        
      2. Update the script with the class and methods.

        1. Replace CLASS with the class of the object to create.
        2. Replace QUERY_CONDITION_METHOD with the method to add a condition to the query.
        3. Replace RUN_QUERY_METHOD with the method to run the query.
        4. 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.

    6. Click the Submit button.

  4. Test the Business Rule.

    1. Create an Incident with Rick Berzle as the Caller.
      1. Use the Application Navigator to open Incident > Create New.
      2. Configure the Incident.
        1. Caller: Rick Berzle
        2. Short description: Received phishing email
      3. Click the Submit button. An information message lists active Incidents with Rick Berzle as the Caller.

The Info Message lists one active Incident for Rick Berzle.

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.

  1. Set up a Configuration Item with tasks to use for testing.
    1. Use the Application Navigator to open Incident > Open.
    2. Click the Personalize List Personalize List icon icon.
    3. Add the Configuration item column to the Selected list in the location of your choice.
    4. Sort by Configuration Item so tasks with no Configuration Item are at the top of the list.
    5. Open the first Incident with no Configuration item.
    6. Set the Configuration item value to *JEMPLOYEE-IBM.
    7. Click the Additional actions button and select the Create Favorite menu item to easily return to the Incident later.
    8. Click the Additional actions button and select the Save menu item.
  2. Create a Business Rule for the Configuration Item table.

    1. Click the Preview this record button for the Configuration item.
    2. Click the Open Record button in the preview.
    3. Click the Additional actions button and select the Configure > Business Rules menu item.
    4. Click the New button.
    5. Configure the Business Rule.
      1. Name: Update Tasks on CI Deactivation
      2. Table: Configuration Item [cmdb_ci]
      3. Advanced: selected (checked)
    6. Configure the When to run section.
      1. When: before
      2. Insert: selected (checked)
      3. Update: selected (checked)
      4. Filter Conditions: [Status] [Changes from] [Installed]
    7. Configure the Advanced section.

      1. 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);
        
        
      2. Update the script with the object and methods.

        1. Replace TABLE with the table to query.
        2. Replace COMPARISON_FIELD with the field on a task record for the Configuration item.
        3. 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.

    8. Click the Submit button.

  3. Test the Business Rule.

    1. Add the Status field to the Computer form.
    2. Find and open the Computer named *JEMPLOYEE-IBM.
    3. Set the Status for *JEMPLOYEE-IBM to Retired.
    4. From the Favorites in the Application Navigator, select the Incident previously saved to the favorites.
    5. Find the message *JEMPLOYEE-IBM is no longer active in the Activities.

The no longer active message displayed in the Activities.

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:

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_id

The 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);



Comments