Working with queries in C/AL
We can run a query and retrieve data using the C/AL code. To achieve this, NAV provides several functions. This recipe will demonstrate a simple example of executing a query using C/AL code.
How to do it...
Follow the steps from the Using a query to extract data recipe to create a query and save it as
Customer Balance
.Create a new codeunit with Object Designer.
Add the following local variable to the
run
trigger:Name
Type
Subtype
CustBalance
Query
Customer Balance
Add the following code into the
OnRun
trigger of the codeunit:CustBalance.TOPNUMBEROFROWS(2); CustBalance.SETFILTER(Balance, '>10000'); CustBalance.OPEN; WHILE CustBalance.READ DO BEGIN MESSAGE('Customer Name : %1 \Balance : %2', CustBalance.Name,CustBalance.Balance); END; CustBalance.CLOSE;
Save and close the codeunit.
On executing the codeunit, you should see a window similar to the one shown in the following screenshot:
How it works...
We use the dataset provided by the Customer Balance...