The following walkthrough demonstrates debugging an action on a page with Visual Studio. If you customize or create new objects for the RoleTailored client, then consider debugging those objects. You use Visual Studio to debug objects that run in the RoleTailored client. For more information, see Debugging in Visual Studio.
About This Walkthrough
This walkthrough illustrates the following tasks:
-
Creating a codeunit and creating an action on a page that runs that codeunit.
-
Enabling Microsoft Dynamics NAV 2009 for debugging.
-
Viewing the C# files that are created for Microsoft Dynamics NAV objects in Visual Studio.
-
Attaching Visual Studio to the Microsoft Dynamics NAV Server service.
-
Adding a breakpoint to a codeunit and running the action on the page with the breakpoint set.
Prerequisites
To complete this walkthrough, you will need:
-
Microsoft Dynamics NAV 2009.
-
The CRONUS International Ltd. demo data company.
-
Visual Studio 2008 SP1 or Visual Studio 2005 SP1 on the computer running Microsoft Dynamics NAV Server.
Creating the Action on a Page
You will first create a codeunit. Then you will create an action on a page. The OnRun trigger of this action calls the codeunit.
To create the codeunit
-
In Object Designer, click Codeunit, and then click New.
-
On the View menu, click C/AL Globals.
-
On the Variables tab, on a new line in the Name field, enter OK. In the Data Type field, select Boolean.
-
On the Variables tab, on a new line in the Name field, enter MyCustomer. In the Data Type field, select Record. In the Subtype field, select Customer from the Table List. Close the C/AL Globals window.
-
In the OnRun trigger of the codeunit, add the following code.
Copy Code OK := MyCustomer.FINDFIRST; MESSAGE('Debug test');
-
On the File menu, click Save to save the new codeunit.
-
In the Save As window, in the ID field, enter 50001. In the Name field, enter Debug Test. Select Compiled, and then click OK.
To create an action that runs a codeunit
-
Create an action on page 22, Customer List. For more information, see How to: Add Actions to a Page. This action has the following characteristics:
-
The caption is Debug Test.
-
The type is Action.
-
It does not have a SubType.
-
It is under the Action group that has the caption Customer.
-
-
Select Action Designer. On the View menu, click C/AL Code.
-
In the C/AL Editor, in the OnAction trigger for the action that you created in step 1, add the following code.
Copy Code CODEUNIT.RUN(50001);
-
Close the C/AL Editor. Close Action Designer.
-
Select Page Designer. On the File menu, click Save.
-
In the Save window, select Compiled, and then click OK.
Enabling Microsoft Dynamics NAV 2009 for Debugging
To debug an action on a page, you must enable Microsoft Dynamics NAV 2009 for debugging.
To enable Microsoft Dynamics NAV 2009 for debugging
-
On the computer running Microsoft Dynamics NAV Server, open the CustomSettings.config file. This file is typically located at %programfiles%\Microsoft Dynamics NAV\60\service.
-
Change the value of the EnableDebugging key to true:
Copy Code <add key="EnableDebugging" value="true" />
-
Save and close the CustomSettings.config file.
-
In Control Panel, click Administrative Tools, and then click Services to open the Services snap-in.
Note If you are running Windows Server 2003, then you click Administrative Tools, and then click Component Services.
-
In the list of services, right-click Microsoft Dynamics NAV Server, and then click Restart.
Viewing the C# Files
When you start the RoleTailored client with debugging enabled, C# files for every RoleTailored client object are generated. You can view these C# files in Visual Studio.
To view the C# files
-
Start the RoleTailored client.
Note The first time that you start the RoleTailored client with debugging enabled, C# files are generated for every RoleTailored client object. Therefore, it takes longer to start the RoleTailored client. The C# files remain on the disk until the Microsoft Dynamics NAV Server service is restarted. We recommend that you set the value of the EnableDebugging key to false after you finish debugging.
-
Start Visual Studio.
Note On a computer running Windows Vista with User Access Control enabled, you must start Visual Studio with administrator privileges. To do this, click Start, right-click Microsoft Visual Studio, and then click Run as administrator.
-
In Visual Studio, open the Codeunit50001.cs file. To find the location of the C# files, do the following:
-
Click Start, point to All Programs, point to Administrative Tools, and then click Event Viewer.
Note If you are running Windows Server 2003, then you click Start, point to Administrative Tools, and then click Event Viewer.
-
In the Application log, find the entry that has MicrosoftDynamicsNAVServer for its Source and has the following description:
Service: <service name>
Source files for debugging are available under <source file location>
-
Attaching Visual Studio to the Microsoft Dynamics NAV Server Service
Before you start debugging, you must attach Visual Studio to the Microsoft Dynamics NAV Server service.
To attach Visual Studio to the service
-
In Visual Studio, on the Debug menu, click Attach to Process.
-
In the Attach to Process window, select the Show processes from all users check box and the Show processes in all sessions check box.
-
Under Available processes, select Microsoft.Dynamics.Nav.Server.exe from the Available Processes list.
-
Above the Available Processes list, next to Attach to, click Select.
-
In the Select Code Type window, select Debug these code types, select Managed from the list of code types, and then click OK.
Note Debugging T-SQL code types is not supported. If you attach the debugger to T-SQL code while the RoleTailored client is running, then the client does not respond. If you attach the debugger to T-SQL code and then start the RoleTailored client, then you get an error that is similar to the following:
The following SQL Server error or errors occurred:
2812,"42000",[Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure 'sp_sdidebug'.
5703,"01000",[Microsoft][ODBC SQL Server Driver][SQL Server]Changed language setting to us_english.
0,"01000",[Microsoft][ODBC SQL Server Driver]SQL Debugging disabled
5701,"01000",[Microsoft][ODBC SQL Server Driver][SQL Server]Changed database context to 'master'.
-
In the Attach to Process window, click Attach.
Adding a Breakpoint to the Codeunit and Running the Code
You use Visual Studio to set breakpoints and step through the C# code.
To add a breakpoint and run code with the breakpoint set
-
In Visual Studio, right-click the following line of code, point to Breakpoint, and then click Insert Breakpoint.
Copy Code Ok = myCustomer.Target.ALFindFirsts(DataError.TrapError);
Note If you have not run the application after the C# files were generated, then you will get the following warning: The breakpoint will not currently be hit. No symbols have been loaded for this document. This warning means that the C# file is not associated with the application in memory. You can ignore the warning. When you run the application to debug, you will hit the breakpoint.
-
In Visual Studio, right-click the following line of code, point to Breakpoint, and then click Insert Breakpoint.
Copy Code NavDialog.ALMessage(new Guid(50001, 0, 39167, 3, 5, 0, 0, 131, 107, 210, 210), @"Debug test");
-
In the RoleTailored client, in the navigation pane, click Customers to go to the Customer List page.
-
On the Related Information menu, under Customer, click Debug Test. The focus changes to the Visual Studio window, and the line of code with the breakpoint is selected.
-
Right-click myCustomer in the line of code, and then click Add Watch.
-
On the Debug menu, point to Windows, point to Watch, and then click Watch1.
-
In the Watch 1 window, expand myCustomer, and then expand Fields to view the values of the fields of the record in the Customer table. Before the line of code runs, the record has not been retrieved and the fields are blank.
-
In Visual Studio, on the Debug menu, click Continue. The code runs until it hits the second breakpoint. Notice that after the record is found, the fields of the MyCustomer variable in the Watch 1 window are populated with the values from the record.
-
In Visual Studio, on the Debug menu, click Continue. Focus returns to the RoleTailored client, and the message window appears.
See Also
© 2010 Microsoft Corporation. All rights reserved.