Walkthrough: Debugging an Action on a Page

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

  1. In Object Designer, click Codeunit, and then click New.

  2. On the View menu, click C/AL Globals.

  3. On the Variables tab, on a new line in the Name field, enter OK. In the Data Type field, select Boolean.

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

  5. In the OnRun trigger of the codeunit, add the following code.

    OK := MyCustomer.FINDFIRST;
    MESSAGE('Debug test');
    
  6. On the File menu, click Save to save the new codeunit.

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

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

    Dd568715.Local_-480267813_nav_debug_addaction(en-us,NAV.60).png

  2. Select Action Designer. On the View menu, click C/AL Code.

  3. In the C/AL Editor, in the OnAction trigger for the action that you created in stepĀ 1, add the following code.

    CODEUNIT.RUN(50001);
    

    Dd568715.Local_-1676436901_nav_debug_onaction(en-us,NAV.60).png

  4. Close the C/AL Editor. Close Action Designer.

  5. Select Page Designer. On the File menu, click Save.

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

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

  2. Change the value of the EnableDebugging key to true:

    <add key="EnableDebugging" value="true" />
    
  3. Save and close the CustomSettings.config file.

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

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

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

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

  3. In Visual Studio, open the Codeunit50001.cs file. To find the location of the C# files, do the following:

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

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

  1. In Visual Studio, on the Debug menu, click Attach to Process.

  2. In the Attach to Process window, select the Show processes from all users check box and the Show processes in all sessions check box.

  3. Under Available processes, select Microsoft.Dynamics.Nav.Server.exe from the Available Processes list.

  4. Above the Available Processes list, next to Attach to, click Select.

  5. 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'.

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

  1. In Visual Studio, right-click the following line of code, point to Breakpoint, and then click Insert Breakpoint.

    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.

  2. In Visual Studio, right-click the following line of code, point to Breakpoint, and then click Insert Breakpoint.

    NavDialog.ALMessage(new Guid(50001, 0, 39167, 3, 5, 0, 0, 131, 107, 210, 210), @"Debug test");
    
  3. In the RoleTailored client, in the navigation pane, click Customers to go to the Customer List page.

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

    Dd568715.Local_-443956254_nav_debug_breakpointhit(en-us,NAV.60).png

  5. Right-click myCustomer in the line of code, and then click Add Watch.

  6. On the Debug menu, point to Windows, point to Watch, and then click Watch1.

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

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

  9. In Visual Studio, on the Debug menu, click Continue. Focus returns to the RoleTailored client, and the message window appears.

See Also

Concepts

Debugging C/AL Code