Debugging in Business Central

We all at certain point in development need to debug the AL Code. Debugging is useful in finding the bugs in our code, by doing debugging we can analyse the code step by step. 


Usually like any other programming language AL language also contains functions(procedures), variables. Since AL programming is an object based language we have records, tables, pages etc which contains fields, these fields will have certain values.

Table: In business central table holds all the data.

Record: Each row in table is called as record.

Page: UI which shows the data which is in the table.

Report: This object is used to generate reports which will have comprehensive data.

Codeunit: This object is used mainly to have logic(code) inside it.


To start debugging we need to first understand about breakpoints, a breakpoint will be set on a specific line of code, which will stop execution of code at that line while debugging. We can start analysing the code from this point.


Breakpoint is set at line 12



To start debugging we can open command palette (Ctrl + Shift + P)

When you select publish without debugging, this will only publish extension to the BC server and you won't be able to debug the code
When you select debug without publishing, this won't publish your app(I,e if you have added any new changes in the code it won't consider them) and debugging will be started.

When you select Publish with Debugging, this will publish the latest app(if you have any new changes) to the BC server and starts debugging.

When you select Rapid Application Publish without debugging, this will only publish your app to the BC server and you won’t be able to debug it. The main difference between rapid application publish and normal publish is that when we choose Rapid Application Publish it will compile the objects which has new changes and use the rest of the objects without compiling again. When you have large number of files in your project this type of publishing is very useful.
When you select Rapid Application Publish with debugging, this will rapid publish the app and starts debugging.

You can also use shortcut directly.

Once you start debugging your AL application in VS Code we can see the following like interface 


Continue: When you press continue it will stop at the next break point in the process of execution.

Step Over: When you click on step over it will execute the current line of code(this code can be a function call, it will execute all the code) and goes to the next line of code.

Step Into: When you click on step into if the current line of code contains a function call then it will go inside that function, for example in the above image at line 12 we have a function call I,e we are calling the function CheckItemIsBlocked so if you press step into, it will go inside the function CheckItemIsBlocked 

Example: 



Now the debugger is inside the function CheckItemIsBlocked


Step out: When you do step out it will execute all the logic inside the current function and goes to line of code from where it is called.

Example: After clicking on step over


The debugger came back to line 12 where the function call was done. Now if you do step into it will go to next line because line 12 is already executed when did step into for the first time

Restart: This will restart the debugging session

Stop: This will stop the debugging 


In the first image on the left side we have Variables, Watch, Call Stack, Breakpoints.


Variables:  In AL Programming language we can have local and global variables. The variables which are global to the whole object can be seen in Global section, the variables which are local (I,e declared inside the function) if the debugger is inside a function we can see all the local variables of this function in local section. The variables related to BC database can be seen in Database Statistics.


Watch: If you do add to watch to any variable or record that will be shown in watch section




Call Stack: Call Stack the contains all the function call till the current line where the debugger is


In the above image the first function call is done from OnafterValidate Trigger to the function CheckItemIsBlocked.

BreakPoints: This will show all the break points that we have set in the current project.



Comments