Debugging business rules

  • Release version: Xanadu
  • Updated August 1, 2024
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Debugging Business Rules

    Debugging business rules in ServiceNow is supported by built-in tools and scripting techniques that help you identify and resolve issues efficiently. Understanding how to leverage these tools, verify variables, and locate relevant data is essential for troubleshooting and ensuring your business rules function as intended.

    Show full answer Show less

    Debugging Tools

    • System Dictionary: Access all tables in your instance to locate necessary data structures via System Definition > Dictionary.
    • System Log: Use System Logs > System Log to capture alert messages inserted in business rules for tracking execution details.
    • Debug Business Rule (Details): Available under System Diagnostics > Session Debug, this module shows whether business rule conditions are met and values set during execution.
    • Alert Messages: Utilize system functions to print messages to pages, fields, or logs to observe script behavior.
    • Business Rule Examples: Refer to existing scripts for error messages and query examples, including OR queries.
    • GlideRecord Information: Use GlideRecord to query and aggregate data from tables within scripts effectively.

    Verifying Variables and Queries

    To understand your business rule’s behavior, verify that data queries return expected records. For example, use gs.addInfoMessage() to display query results on the screen and confirm whether records exist:

    var rec = new GlideRecord('incident');rec.addQuery('active', true);rec.query();gs.addInfoMessage("This is rec.next: " + rec.next());while (rec.next()) { gs.print(rec.number + ' exists');}

    This method helps isolate and test individual parts of your script, ensuring each component works correctly before combining them.

    Locating Data in Extended Tables

    When debugging, it’s important to understand table relationships, especially with table extensions. For example, sctask extends task, so querying one may require referencing the other by sysid. An example script queries sctask records and then retrieves related task records to access fields like work notes:

    var kids = new GlideRecord('sctask');kids.query();while (kids.next()) { var parents = new GlideRecord('task'); parents.addQuery('sysid', '=', kids.sysid); parents.query(); while(parents.next()) { gs.addInfoMessage("This is task number: " + parents.number); gs.print("This is task number: " + parents.number); gs.addInfoMessage("These are the work notes: " + parents.worknotes); gs.print("These are the work notes: " + parents.worknotes); }}

    Understanding table extensions and sysid usage enables you to accurately locate and validate data your business rules interact with.

    Debugging business rules can be achieved with resources available in the ServiceNow product.

    1. Tools

    The first step in the process is to identify tools which will help you figure out what's wrong.

    Table 1. Debugging tools
    Debugging tool Description
    System Dictionary Navigate to System Definition > Dictionary. The dictionary provides a list of all tables within your instance and can be invaluable when trying to locate information.
    System Log Navigate to System Logs > System Log. You can place alert statements in your business rule which can write information to the log.
    Debug Business Rule (Details) Navigate to System Diagnostics > Session Debug > Debug Business Rule (Details). This debugging module displays the results business rules. Use this module to see if conditions are being met and values are being set as expected.
    Alert Messages There are several system functions that allow you to print messages to the page, the field or the log file. See Scripting alert, info, and error messages.
    Business Rule Examples Sometimes you can find what you're looking for in scripts others have written, including business rule error messages, or by building an OR query.
    GlideRecord Information This is the basic syntax used to query the database for information. See Querying tables in script. GlideRecord also includes aggregation support.

    2. Variables

    The next step is to gain some insight into the behavior of your business rule. For every action except an insert, you will more than likely use a query to get your record(s).

    var rec = new GlideRecord('incident');
    rec.addQuery('active',true); 
    rec.query();
    while (rec.next()) {
     gs.print(rec.number + ' exists');
     }

    To verify whether your query is actually returning records you can use gs.addInfoMessage to display information at the top of the screen.

    var rec = new GlideRecord('incident');
    rec.addQuery('active',true); 
    rec.query();
    gs.addInfoMessage("This is rec.next: " + rec.next());
    while (rec.next()) {
     gs.print(rec.number + ' exists');
     }

    If your query returns no records you see the following:

    This is rec.next: false

    Use this technique to verify every variable within your business rule contains expected values.

    Tip:
    If necessary, break your script down into individual pieces and verify each piece works separate from the whole and then put them all back together one step at a time.

    3. Locating information

    The last step is to make sure you know where to find the information your rule is looking for.

    In the ServiceNow application, one table can extend another table. This means when searching for information, you might need to query the parent table for the extended table's sys_id to find what you seek.

    A good example is the sc_task table, which extends the task table. The script below queries the extended table (sc_task) for the current sys_id and then query the parent table (task) for records with the matching sys_id, and then prints out the work notes field.

    var kids = new GlideRecord('sc_task');
    kids.query();
     
    gs.addInfoMessage("This is requested item number: " + current.number);
    gs.print("This is the requested item number: " + current.number);
     
    while (kids.next()) { 
     var parents = new GlideRecord('task');
     parents.addQuery('sys_id', '=', kids.sys_id);
     parents.query();
     
     while(parents.next()) {
      gs.addInfoMessage("This is task number: " + parents.number);
      gs.print("This is task number: " + parents.number);
      gs.addInfoMessage("These are the work notes: " + parents.work_notes);
      gs.print("These are the work notes: " + parents.work_notes);
      }
     
     }