Share This Post

Business Central

Dynamics 365 Business Central AL Control Statements

AL code consists of one or more Dynamics 365 Business Central AL Control Statements, which are executed sequentially in top-down order. Although, you will often need to control the direct top down flow of the execution.

One or more statements may have to be repeated more than once, or you may have to change the execution of a certain statement conditional. To do this, you use control structures.

The control structures in AL are divided into the following main groups, as described in this article:

  • Compound Statements
  • Conditional Statements
  • Repetitive Statements

Dynamics 365 Business Central AL Control Statements Structures 

1. AL Compound Statements

In some cases, the AL syntax only lets you use a single statement. However, if you have to execute more than one simple statement, the Dynamics 365 Business Central AL Control Statements can be written as a compound statement by enclosing the statements between the begin and end keywords.

begin
<Statement 1>;
<Statement 2>;
..
<Statement n>;
end

The individual statements are separated by a semicolon. Nevertheless, an extra semicolon before an end does not cause an error because it is interpreted by the compiler as an empty statement.

Blocks

The begin-end structure is also called a block. Blocks can be very useful to refer to the other control structures in AL.

When begin follows then, else, or do, it should be on the same line and preceded by one space character.

Example

if (x = y) and (a = b) then begin
x := a;
y := b;
end;

Example

if (xxx = yyyyyyyyyy) and
(aaaaaaaaaa = bbb)
then begin
x := a;
x := y;
a := y;
end else begin
y := x;
y := a;
end;

2. AL Conditional Statements

You use conditional statements to specify a condition and one or more commands to execute if the condition is evaluated as true or false. There are two types of conditional statements in AL:

  • if-then-else, where there are two choices.
  • case, where there are more than two choices.
a. If-then else Statements

if-then-else Dynamics 365 Business Central AL Control Statements have the following syntax.

if <Condition> then
<Statement1>
else
<Statement2>

If <Condition> is true, then <Statement1> is executed. If <Condition> is false, then <Statement2> is executed.

The square brackets around else <Statement2> mean that this part of the statement is optional. The else statement is used when different actions are executed depending on how <Condition> is evaluated.

You can build more complex control structures by nesting if-then-else statements. The following example is a typical if-then-else statement.

if <Condition1> then
if <Condition2> then
<Statement1>
else
<Statement2>

If <Condition1> is false, then nothing is executed. If <Condition1> and <Condition2> are both true, then <Statement1> is executed. If <Condition1> is true and <Condition2> is false, then <Statement2> is executed.

Reading several nested if-then-else Dynamics 365 Business Central AL Control Statements can be very confusing but generally, an else statement belongs to the last if statement that lacks an else statement.

Programming Conventions

  • If and then should be on the same line. else should be on a separate line.
  • If there are many or long expressions, then should be on a new line and be aligned with if.
  • When you write if expressions with then and else parts, write them so that the result is more feasible than the else one.
  • If the last statement in the then part of an if-then-else statement is an exit or an ERROR, do not continue with an else statement.
b. Case Statements

The Case statements have the following syntax.

case <Expression> of
<Value set 1>:
<Statement 1>;
<Value set 2>:
<Statement 2>;

<Value set n>:
<Statement n>;
[else
<Statement n+1>]
end;

In this definition, <Expression> cannot be a record and <Value set> must be an expression or a range.

Case statements are also called multiple option statements and are typically utilized when you must choose between more than two different actions. The method of the case statement is as follows:

  • The <Expression> is evaluated, and the first matching value set executes the associated statement, if there is one.
  • If no value set matches the value of the expression and the optional else part has been omitted, then no action is taken. If the optional else part is used, then the associated statement is executed.

The data type of the value sets must be similar as the data type of <Expression> or at least be convertible to the same data type.

In many cases, the data type of the value sets are changed to the date type of the evaluated expression. Only exception is that, the evaluated expression is a Code variable. If the evaluated expression is a Code variable, then the value sets are not converted to the Code data type.

Programming Conventions

When using a case statement, indent the value sets by two character spaces. If you have two or more value sets on the similar line, then separate them by commas without spaces. The ending value set on a line is instantly followed by a colon without any preceding space.

If there is a begin, then it should be put on a separate line except it follows else. The action starts on the line after the value set and is further indented by two character spaces.

If a begin follows an else, then it should be on the same line as else. If there are more than two alternatives, utilize a case statement. Otherwise, use an if-then-else statement.

3. Repetitive Statements

A repetitive statement is also known as a loop. The following table displays the looping mechanisms in AL.

Looping mechanism Description
for Repeats the inner statement until a counter variable equals the maximum or minimum value specified.
foreach Repeats the inner statement for each statement in a List, XmlNodeList, XmlAttributeCollection, or JsonArray.
while Repeats the inner statement as long as the specified condition is true. The statement in a loop of this kind is replicated 0 or more times.
repeat Repeats the inner statements until the specified conditions evaluate to true. The statements in a loop of this kind are always executed at least one time.
For-to and for-downto Control Structure

The following syntax shows the for-to and for-downto statement.

for <Control Variable> := <Start Number> to <End Number> do
<Statement>

for <Control Variable> := <Start Number> downto <End Number> do
<Statement>

The data type of <Control Variable><Start Number>, and <End Number> must be Boolean, number, time, or date.

Use for-to and for-downto Dynamics 365 Business Central AL Control Statements when you want to execute code a specific number of times. The <Control Variable> controls the number of times that the code of the inner statement is executed according to the following:

  • In a for-to loop statement, the <Control Variable> value is increased by one after each iteration. The inner <Statement> is executed repeatedly until the *<Start Number>* value is greater than the *<End Number>* value.
  • In a for-downto loop statement, the <Control Variable> value is decreased by one after each iteration. The inner <Statement> is executed repeatedly until the <Start Number> value is less than the <End Number> value.
Foreach Control Structure

You can use the foreach statement to iterate through List, XmlNodeList, XmlAttributeCollection, and JsonArray expressions. The foreach statement has the following syntax.

foreach <Element> in <List> do
<Statement>

The <List> variable must be of the List, XmlNodeList, XmlAttributeCollection, or JsonArray type. The <Element> variable must be a data type that is compatible with elements specified by the <List>.

The following code example iterates through a list of customer names and returns each customer name in a message.

procedure PrintCustomerNames(customerNames : List of [Text]);
var
customerName : Text;
begin
foreach customerName in customerNames do
message(customerName);
end;

While-do Control Structure

The following syntax shows the while-do statement.

while <Condition> do
<Statement>

If <Condition> is true, then <Statement> is executed repeatedly until <Condition> becomes false. If <Condition> is false from the start, then <*Statement> is never executed.

The while do statement can be used when some code should be repeated as long as an expression is true.

Repeat-until Control Structure

The following syntax shows the repeat-until statement.

repeat
<Statements> until <Condition>

<Statements> is executed repeatedly until <Condition> is true.

The repeat until control structure resembles the while control structure. The difference is that because the repeat until statement is executed from left to right, the <Statements> is always executed at least one time, regardless of what the <Condition> is evaluated to.

This contrasts with the while control structure, which performs the evaluation before the <Statement> is executed. In the while control structure, if the first evaluation of <Condition> returns false, then no Dynamics 365 Business Central AL Control Statements are executed.

Exit Statement

The exit statement is used to control the flow of the execution. The following syntax shows an exit statement.

exit([<Value>])

An exit statement is used to interrupt the execution of a AL trigger. The interruption occurs even when the code is executed inside a loop or a similar structure. The exit statement is also used when a local method should return a value.

4. Break Statement

You use the break statement to terminate the iterative statement in which it appears.

break;

You typically use the break statement in the repeating statements such as for, while, or repeat to stop an iteration or loop when certain conditions are met.

The break statement is different than the Break Method (Report, XMLport). Even though both stop an iteration or loop, the break method will also terminate the trigger in which it is run. For more information on Microsoft Dynamics 365 Business Central AL Control Statements, please contact us.

Share This Post

Leave a Reply

avatar
  Subscribe  
Notify of
Skip to toolbar