Please use docs.servicenow.com for the latest documentation.

This site is for reference purposes only and may not be accurate for the latest ServiceNow version

Debugging Business Rules

From Wiki Archive
Jump to: navigation, search
Note
Note: This article applies to Fuji. For more current information, see Debugging Business Rules at http://docs.servicenow.com

The ServiceNow Wiki is no longer being updated. Please refer to http://docs.servicenow.com for the latest product documentation.

Overview

Debugging business rules can be achieved with resources available in the ServiceNow product. The Tables and Classes wiki page is a good resource for information regarding table structure. You can also use the System Dictionary to research tables.

Troubleshooting

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

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 > Debug Business Rule (Details). This debugging module displays which fields have changed as a result of 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 fuctions which will allow you to print messages to the page, the field or the log file. They can be found on the Scripting Alert, Info, and Error Messages wiki page.
Business Rule Examples Sometimes you can find what you're looking for in scripts others have written, including business rule error messages, or building an OR query
GlideRecord Information This is the basic syntax used to query the database for information is include dthe GlideRecord wiki pages. GlideRecord also includes Aggregation Support.

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).

<source lang="javascript"> var rec = new GlideRecord('incident'); rec.addQuery('active',true); rec.query(); while (rec.next()) {

gs.print(rec.number + ' exists');
} 

</source>

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

<source lang="javascript"> 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');
} 

</source>

If your query returns no records you will see

This is rec.next: false

Use this technique to verify every variable within your business rule contains expected values. 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.

How To Find It

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

In the Service Now 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 get what you seek. A good example would be the sc_task table, which extents the task table. In the script below I query the extended table(sc_task) for the current sys id and then query the parent table(task) for records with the matching sys id. Then I print out the work notes field I was looking for.

<source lang="javascript"> 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);
 }
} 

</source>