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

ในการพัฒนาโปรแกรมด้วยภาษา AL (AL Programming Language) บน Dynamics 365 Business Central ประสิทธิภาพในการดึงข้อมูลจาก Table เป็นสิ่งที่สำคัญอย่างมาก โดยเฉพาะเมื่อทำงานกับข้อมูลจำนวนมาก SetLoadFields เป็น Method ที่ช่วยให้เราสามารถควบคุมได้ว่าจะโหลดเฉพาะ Field ที่จำเป็นเท่านั้น ส่งผลให้ระบบทำงานได้รวดเร็วขึ้นและใช้ทรัพยากรน้อยลง

การใช้ SetLoadFields() ช่วยลดการโหลด Field ที่ไม่เกี่ยวข้องออกไป เหมาะอย่างยิ่งในกรณีที่เราต้องการใช้งานเฉพาะบาง Field โดยไม่จำเป็นต้องโหลด Field อื่นที่ไม่ได้ใช้งาน ช่วยเพิ่มประสิทธิภาพของระบบทั้งในด้านความเร็วและการประมวลผล

รูปแบบการใช้งาน (Syntax)

Record.SetLoadFields(Field1 [, Field2, Field3, ...]);

Parameters

  • Record: ชนิดข้อมูล Record เช่น Customer, Item
  • Field1, Field2, ...: ระบุชื่อ Field ที่ต้องการ

หมายเหตุ

  • SetLoadFields จะต้องถูกเรียกก่อน FindSet, FindFirst, FindLast, Find, หรือ Get
  • หากคุณไม่ได้ใช้ SetLoadFields, ระบบจะโหลด Field ทั้งหมดโดยอัตโนมัติ

ตัวอย่างวิธีการทดสอบการใช้งาน SetLoadFields

เพื่อทดสอบว่า SetLoadFields ช่วยให้ระบบโหลดข้อมูลเร็วขึ้นจริงหรือไม่ เราสามารถใช้ Time และ Duration ในการจับเวลาการโหลดข้อมูลจาก Table ขนาดใหญ่ เช่น Item Ledger Entry ได้

ตัวอย่างแบบไม่ใช้ SetLoadFields

    procedure TestWithoutSetLoadFields(): Duration
    var
        ItemLedgEntry: Record "Item Ledger Entry";
        StartTime: Time;
        EndTime: Time;
        RemainingQty: Decimal;
        InvoicedQty: Decimal;
        Quantity: Decimal;
    begin
        StartTime := Time;
        ItemLedgEntry.Reset();
        if ItemLedgEntry.FindSet() then
            repeat
                RemainingQty += ItemLedgEntry."Remaining Quantity";
                InvoicedQty += ItemLedgEntry."Invoiced Quantity";
                Quantity += ItemLedgEntry.Quantity;
            until ItemLedgEntry.Next() = 0;

        EndTime := Time;
        exit(EndTime - StartTime);
    end;

ตัวอย่างแบบใช้ SetLoadFields

    procedure TestWithSetLoadFields(): Duration
    var
        ItemLedgEntry: Record "Item Ledger Entry";
        StartTime: Time;
        EndTime: Time;
        RemainingQty: Decimal;
        InvoicedQty: Decimal;
        Quantity: Decimal;
    begin
        StartTime := Time;
        ItemLedgEntry.Reset();
        ItemLedgEntry.SetLoadFields("Remaining Quantity", "Invoiced Quantity", Quantity);
        if ItemLedgEntry.FindSet() then
            repeat
                RemainingQty += ItemLedgEntry."Remaining Quantity";
                InvoicedQty += ItemLedgEntry."Invoiced Quantity";
                Quantity += ItemLedgEntry.Quantity;
            until ItemLedgEntry.Next() = 0;

        EndTime := Time;
        exit(EndTime - StartTime);
    end;

สรุป (Summary)

SetLoadFields เป็นเทคนิคง่าย ๆ สำหรับการเพิ่มประสิทธิภาพการโหลดข้อมูล ช่วยให้เราดึงเฉพาะ Field ที่จำเป็นเข้ามาใช้งาน ไม่ต้องโหลดข้อมูลทั้งหมดของ Record ซึ่งช่วยลดภาระของระบบ ทำให้โค้ดทำงานได้เร็วขึ้น และใช้ได้ดีเมื่อเราทำงานกับข้อมูลจำนวนมากใน Table ที่มีข้อมูลจำนวนมาก

ข้อมูลเพิ่มเติมได้ที่นี่

🔗 Record.SetLoadFields Method – Microsoft Learn

Leave a comment