Checking for conditions using an IF statement
Sometimes, we want to execute a section of code on a specific condition; this recipe will help to explain the syntax for the same.
How to do it...
Let's create a new codeunit from Object Designer.
Add the following global variables:
Name
Type
SubType
SalesHeader
Record
Sales header
RecordsProcessed
Integer
Now write the following code in the
OnRun
trigger of the codeunit: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 to complete the task.
On execution of the codeunit, you should see a window similar to 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 of the previous code does. It tells the code that if you find some records, then it should do...