How to use CASE statement in D365 BC

CASE statement in D365 BC:

(1) Definition- Case statements are typically used when you must choose between more than two different actions.

(2) Syntax of “CASE” statement-

case <Expression> of 
<Value set 1>: 
<Statement 1>; 
<Value set 2>: 
<Statement 2>; 
 
<Value set n>: 
<Statement n>; 
[else 
<Statement n+1>] 
end;

(3) Lets take an example, get name of the month from Date:

get name of the month from Date Code in Business Central

(4) The <Expression> is evaluated, and the first matching value set executes the associated statement, if there is one.

(5) 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.

(6) In the above example, 1 is consider as first month name and so on.

Source Code as below:

codeunit 50000 “Custom Functions”
{
    trigger OnRun()
    begin
    end;
 
    var
        myInt: Integer;
 
    procedure GetNameOfMonthFromDate(var GetDate: Date): Text[10]
    var
        ModValue: Decimal;
        NameOfMonth: Text[10];
        GetMonth: Integer;
    begin
        GetDate := Today; //Assign Date value to variable
        GetMonth := Date2DMY(GetDate, 2); //Return Month value form Date
        Case GetMonth of
            1:
                NameOfMonth := ‘JAN’;
2:
                NameOfMonth := ‘FEB’;
            3:
                NameOfMonth := ‘MAR’;
            4:
                NameOfMonth := ‘APR’;
            5:
                NameOfMonth := ‘MAY’;
            6:
                NameOfMonth := ‘JUN’;
            7:
                NameOfMonth := ‘JUL’;
            8:
                NameOfMonth := ‘AUG’;
            9:
                NameOfMonth := ‘SEP’;
            10:
                NameOfMonth := ‘OCT’;
            11:
                NameOfMonth := ‘NOV’;
            12:
                  NameOfMonth := ‘DEC’;
        End;
        EXIT(NameOfMonth);
    end;
   
}

Skip Sales Order Document deletion when it is fully posted
Skip Purchase Order Document deletion when it is fully posted
How to use FOR-TO loop statement in Microsoft D365
How to Find Base Application Object in BC D365
AL Control Statements– Microsoft Docs

Leave a Reply