{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":658,"path":"api-reference/scripts/r_DebuggingBusinessRules.md","title":"Debugging business rules","bundle":null,"doc_type":null,"release":"australia","body":"\n# Debugging business rules\n\nDebugging business rules can be achieved with resources available in the ServiceNow product.\n\n## 1. Tools\n\nThe first step in the process is to identify tools which will help you figure out what's wrong.\n\n|Debugging tool|Description|\n|--------------|-----------|\n|System Dictionary|Navigate to **System Definition** &gt; **Dictionary**. The dictionary provides a list of all tables within your instance and can be invaluable when trying to locate information.|\n|System Log|Navigate to **System Logs** &gt; **System Log**. You can place alert statements in your business rule which can write information to the log.|\n|Debug Business Rule \\(Details\\)|Navigate to **System Diagnostics** &gt; **Session Debug** &gt; **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.|\n|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](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/r_ScriptingAlertInfoAndErrorMsgs.md).|\n|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.|\n|GlideRecord Information|This is the basic syntax used to query the database for information. See [Querying tables in script](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/c_UsingGlideRecordToQueryTables.md). GlideRecord also includes aggregation support.|\n\n## 2. Variables\n\nThe 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\\).\n\n```\nvar rec = new GlideRecord('incident');\nrec.addQuery('active',true); \nrec.query();\nwhile (rec.next()) {\n gs.print(rec.number + ' exists');\n }\n```\n\nTo verify whether your query is actually returning records you can use *gs.addInfoMessage* to display information at the top of the screen.\n\n```\nvar rec = new GlideRecord('incident');\nrec.addQuery('active',true); \nrec.query();\ngs.addInfoMessage(\"This is rec.next: \" + rec.next());\nwhile (rec.next()) {\n gs.print(rec.number + ' exists');\n }\n```\n\nIf your query returns no records you see the following:\n\n```\nThis is rec.next: false\n```\n\nUse this technique to verify every variable within your business rule contains expected values.\n\n**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.\n\n## 3. Locating information\n\nThe last step is to make sure you know where to find the information your rule is looking for.\n\nIn 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.\n\nA 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.\n\n```\nvar kids = new GlideRecord('sc_task');\nkids.query();\n \ngs.addInfoMessage(\"This is requested item number: \" + current.number);\ngs.print(\"This is the requested item number: \" + current.number);\n \nwhile (kids.next()) { \n var parents = new GlideRecord('task');\n parents.addQuery('sys_id', '=', kids.sys_id);\n parents.query();\n \n while(parents.next()) {\n  gs.addInfoMessage(\"This is task number: \" + parents.number);\n  gs.print(\"This is task number: \" + parents.number);\n  gs.addInfoMessage(\"These are the work notes: \" + parents.work_notes);\n  gs.print(\"These are the work notes: \" + parents.work_notes);\n  }\n \n }\n```\n\n**Parent Topic:**[Debugging scripts](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/scripts/script-debug-overview.md)\n\n"}}