Checking for conditions using an IF statement
Some code should only be executed when certain conditions occur. This recipe will show you how to write code to make that decision.
How to do it...
Create a new codeunit from Object Designer.
Add the following global variables:
Name
Type
Subtype
SalesHeader
Record
Sales Header
RecordsProcessed
Integer
Write the following code in the
OnRun
trigger:IF SalesHeader.FINDSET THEN BEGIN REPEAT RecordsProcessed += 1; UNTIL SalesHeader.NEXT = 0; MESSAGE('Processed %1 records.', RecordsProcessed); END ELSE MESSAGE('No records to process.');
Save and close the codeunit.
When you run the codeunit you will see a window like the one shown in the following screenshot:
How it works...
In order to execute the code that processes the records, there must be records in the table. That's exactly what the first line does. It tells the code that IF you find some records THEN it should do these actions. In this case, the action is to count the records in the...