In this walkthrough you will learn how to use Report Designer to create a document. The example used is a simple sales invoice that does not take the complexities of VAT calculations into account and does not test a number of conditions that would have to be tested in a real-life situation. Furthermore, it does not print all the information you would expect to find on an invoice.
Prerequisites
To complete this walkthrough, you will need:
-
Microsoft Dynamics NAV installed with a developer license.
-
The CRONUS International Ltd. demo data company installed.
Defining the Data Model
The two primary tables used in creating a sales invoice are the Sales Invoice Header and the Sales Invoice Line tables. Two supporting tables, Payment Terms and Shipment Method, are used to expand the code used in the invoice tables to more descriptive text. The Company Information table is used to retrieve information about the company that is preparing the invoice.
The Sales Invoice Header table contains general information about each sales invoice that has been posted, while the Sales Invoice Line table contains the individual lines that make up each invoice. The tables are related through a field called No. in the header table (the primary key of this table) and Document No. in the lines table.
To define the data model
-
Open Object Designer and create a new blank report.
-
In Report Designer, create a data item based on the Sales Invoice Header table.
-
Create another data item based on the Sales Invoice Line table, and indent this data item one level.
-
Open the Properties window of the Sales Invoice Line data item. By default, the DataItemLinkReference property of the Sales Invoice Line data item points to the Sales Invoice Header data item. Leave it like this, and set the value of DataItemLink property to
Document No.=FIELD(No.)
. -
Enter the Amount field as the value of the TotalFields property of the Sales Invoice Line data item. This calculates the total amount for all the lines in the invoice.
-
Finally, change to the Properties window of the Sales Invoice Header data item. Enter the No. field as the value of the ReqFilterFields property. This lets the users of the report select a posted invoice to print.
Creating the Variables
The data model has now been defined. In this report, some supporting variables are needed to access information from tables that cannot be fitted into the data model.
To create the variables
-
Click View, and then click C/AL Globals.
-
Declare the following variables in the C/AL Globals window.
Name DataType Subtype Length CompInfo
Record
Company Information
PaymentTerms
Record
Payment Terms
ShipmentMethod
Record
Shipment Method
CustAddr
Text
30
CompAddr
Text
30
-
The two last variables must be declared as arrays. Open the Properties window of each variable and set Dimensions to 6 for the CustAddr variable and to 4 for the CompAddr variable.
The data model has now been defined. Next, you need to add C/AL code to the report triggers.
Writing Code for the Triggers
This basic version of the report needs a very limited amount of C/AL code in order to function correctly. The code must be entered in the triggers of the Sales Invoice Header data item.
To write code for the triggers
-
Select the Sales Invoice Header data item, click View, and then click C/AL Code.
-
In the OnPreDataItem() trigger, enter the following lines of code.
Copy Code CompInfo.GET; CompAddr[1] := CompInfo.Name; CompAddr[2] := CompInfo.Address; CompAddr[3] := CompInfo."Address 2"; CompAddr[4] := CompInfo.City; COMPRESSARRAY(CompAddr);
-
In the OnAfterGetRecord() trigger, enter the following lines of code.
Copy Code CustAddr[1] := "Bill-to Name"; CustAddr[2] := "Bill-to Name 2"; CustAddr[3] := "Bill-to Contact"; CustAddr[4] := "Bill-to Address"; CustAddr[5] := "Bill-to Address 2"; CustAddr[6] := "Bill-to City"; COMPRESSARRAY(CustAddr); PaymentTerms.GET("Payment Terms Code"); ShipmentMethod.GET("Shipment Method Code")
The code in the OnPreDataItem
trigger works like
this:
-
The first line,
CompInfo.GET
, retrieves a record, which is the only record from the Company Information table. -
The next four lines assign the contents of a field in the record in the Company Information table to an element of the
CompAddr
array. -
The final line of the
OnPreDataItem
trigger uses theCOMPRESSARRAY
function with theCompAddr
array as an argument to eliminate empty elements from the array. You do this because you cannot be certain that all the fields in the retrieved record contain values. If you printed each field on a separate line, an empty field would cause an empty line to be printed.
The code in the OnAfterGetRecord
trigger works like
this:
-
The first six lines assign values from the record in the Sales Invoice Line data item to elements of the
CustAddr
array. -
After this,
COMPRESSARRAY
is used to eliminate empty elements from the array as described earlier. -
The last two lines use the
GET
function (with the code for Payment Terms and Shipment Method from the Sales Invoice Header record as arguments) to retrieve the related records from the Payment Terms and Shipment Method tables. When you design the sections, the full text descriptions can then be extracted from these records.
Designing the Sections
Now that you have defined the data model and added C/AL code to retrieve supporting information, you can design the sections. For information about how to design sections in a report, see Walkthrough: Creating Advanced Sample Reports.
See Also
© 2010 Microsoft Corporation. All rights reserved.