Dynamics 365 Business Central: การใช้งาน AL Control Statements ใน AL Programming

ใน AL Programming การใช้งาน Control Statements เป็นสิ่งที่จำเป็นสำหรับการควบคุมการทำงานของโปรแกรม AL คำสั่งเหล่านี้ช่วยให้โปรแกรมเมอร์สามารถเขียนโค้ดที่ตอบสนองตามเงื่อนไขต่างๆ ดำเนินการคำสั่งที่หลากหลาย หรือทำซ้ำการทำงานตามจำนวนครั้งที่กำหนดได้ Control Statements แบ่งออกเป็นสามประเภทหลักๆ ดังนี้: Compound Statements, Conditional Statements, และ Repetitive Statements

1. AL Compound Statements

AL Compound Statements คือการรวมคำสั่งหลายๆ คำสั่งไว้ในกลุ่มเดียวกัน หรือบล็อกคำสั่ง โดยใช้ begin และ end คำสั่งเหล่านี้จะถูกดำเนินการตามลำดับ

ตัวอย่าง

procedure UpdateCustomerInfo(CustomerNo: Code[20])
var
    Customer: Record Customer;
begin
    if Customer.Get(CustomerNo) then begin
        Customer.Name := 'New Customer Name';
        Customer.Address := '123 New Street';
        Customer.City := 'Bangkok';
        Customer.Modify(true);
    end else
        Error('Customer not found.');
end;

อธิบาย: ในตัวอย่างนี้ begin และ end ถูกใช้เพื่อรวมหลายคำสั่งไว้ในบล็อกเดียว ซึ่งช่วยให้สามารถทำงานหลายคำสั่ง เช่น การเปลี่ยนชื่อ ที่อยู่ และเมืองของลูกค้า ภายในขั้นตอนเดียว

2. AL Conditional Statements

AL Conditional Statements ใช้ในการตัดสินใจว่าคำสั่งใดจะถูกดำเนินการโดยอ้างอิงจากเงื่อนไขที่กำหนดไว้ Conditional Statements ใน AL ประกอบด้วย If-Then-Else และ Case.

2.1 If-Then-Else Statement

If-Then-Else ใช้ในการตรวจสอบเงื่อนไขและดำเนินการคำสั่งตามเงื่อนไขนั้น ถ้าเงื่อนไขเป็นจริงจะดำเนินการคำสั่งในส่วน Then แต่ถ้าไม่เป็นจริงจะดำเนินการในส่วน Else (ถ้ามี)

ตัวอย่าง

procedure CalculateShippingFee(Weight: Decimal): Decimal
var
    ShippingFee: Decimal;
begin
    if Weight <= 5 then
        ShippingFee := 50.00
    else if Weight <= 10 then
        ShippingFee := 100.00
    else
        ShippingFee := 150.00;

    exit(ShippingFee);
end;

อธิบาย: ในตัวอย่างนี้ ค่าธรรมเนียมการขนส่งจะถูกคำนวณตามน้ำหนักของพัสดุ โดยใช้ If-Then-Else statement ถ้าน้ำหนักน้อยกว่า 5 กิโลกรัม ค่าธรรมเนียมจะเป็น 50 บาท ถ้าอยู่ระหว่าง 5-10 กิโลกรัม จะเป็น 100 บาท และถ้ามากกว่า 10 กิโลกรัมจะเป็น 150 บาท

2.2 Case Statement

Case ใช้ในการเลือกคำสั่งที่เหมาะสมจากหลายๆ ตัวเลือก โดยอ้างอิงจากค่าของ Expression ที่กำหนดไว้

ตัวอย่าง

procedure GetCustomerCategory(CustomerPoints: Integer): Text
var
    Category: Text;
begin
    case CustomerPoints of
        0..100:
            Category := 'Bronze';
        101..500:
            Category := 'Silver';
        501..1000:
            Category := 'Gold';
        else
            Category := 'Platinum';
    end;

    exit(Category);
end;

อธิบาย: ในตัวอย่างนี้ Case statement ถูกใช้เพื่อกำหนดประเภทของลูกค้าตามคะแนนที่ลูกค้าได้สะสมไว้ ถ้าคะแนนอยู่ระหว่าง 0-100 จะถูกจัดเป็น Bronze, 101-500 จะเป็น Silver, 501-1000 จะเป็น Gold และมากกว่า 1000 จะเป็น Platinum

3. AL Repetitive Statements

AL Repetitive Statements หรือที่เรียกว่าการทำซ้ำ ใช้ในการทำงานซ้ำๆ จนกว่าจะถึงเงื่อนไขที่กำหนดไว้ ประกอบด้วย For, While, และ Repeat-Until.

3.1 For Loop

For Loop ใช้ในการทำซ้ำคำสั่งตามจำนวนครั้งที่กำหนด โดยใช้ตัวแปรควบคุมการนับ

ตัวอย่าง

procedure CalculateTotalItems(NumItems: Integer): Integer
var
    i: Integer;
    Total: Integer;
begin
    Total := 0;
    for i := 1 to NumItems do
        Total := Total + 1;

    exit(Total);
end;

อธิบาย: ในตัวอย่างนี้ For Loop ถูกใช้ในการนับจำนวนรายการทั้งหมด โดยลูปจะทำซ้ำจาก 1 จนถึงค่าที่กำหนด (NumItems) และเพิ่มค่า Total ทีละ 1 ในแต่ละรอบลูป

3.2 While Loop

While Loop ใช้ในการทำซ้ำคำสั่งตราบใดที่เงื่อนไขยังคงเป็นจริง โดยตรวจสอบเงื่อนไขก่อนการทำงาน

ตัวอย่าง

procedure CalculateInventory(AvailableQty: Decimal): Decimal
var
    NeededQty: Decimal;
begin
    NeededQty := 100;
    while AvailableQty < NeededQty do begin
        AvailableQty := AvailableQty + 10;
    end;

    exit(AvailableQty);
end;

อธิบาย: ในตัวอย่างนี้ While Loop จะทำการเพิ่มจำนวนสินค้าคงเหลือ (AvailableQty) ทีละ 10 จนกว่าจะเพียงพอต่อความต้องการ (NeededQty)

3.3 Repeat-Until Loop

Repeat-Until Loop คล้ายกับ While Loop แต่จะตรวจสอบเงื่อนไขหลังจากที่ทำงานไปแล้วอย่างน้อยหนึ่งครั้ง

ตัวอย่าง

procedure CalculateTotalOutstandingBalance(var CustLedgerEntries: Record "Cust. Ledger Entry"): Decimal
var
    TotalBalance: Decimal;
begin
    TotalBalance := 0;
    if CustLedgerEntries.FindSet() then begin
        repeat
            TotalBalance := TotalBalance + CustLedgerEntries.Amount;
        until CustLedgerEntries.Next() = 0;
    end;
    
    exit(TotalBalance);
end;

อธิบาย: ในตัวอย่างนี้ เราใช้ Repeat-Until Loop เพื่อวนซ้ำใน Record Cust. Ledger Entry เพื่อคำนวณยอดรวมคงเหลือ (TotalBalance) โดยจะทำการบวกยอดในแต่ละบรรทัดของ CustLedgerEntries.Amount จนกว่าจะสิ้นสุด Record (Next() = 0)

สรุป

การใช้ AL Control Statements ไม่ว่าจะเป็น Compound, Conditional, หรือ Repetitive Statements เป็นเครื่องมือสำคัญในการควบคุมการทำงานของโปรแกรม ช่วยให้โปรแกรมเมอร์สามารถสร้างโค้ดที่มีความยืดหยุ่นและตอบสนองต่อสถานการณ์ต่างๆ ได้อย่างมีประสิทธิภาพ

Leave a comment