在 Business Central 的 Report(AL) 中查詢相關連資訊

需求: 在Business Central 的Report程式中, 如何利用銀行代號(Bank Account No.)取得銀行名稱, 利用客戶編號(Bal. Account No.)取得客戶名稱

Business Central 的 Report(AL) 中,要:

  1. 由銀行代號 (Bank Account No.) 取得銀行名稱
  2. 由客戶編號 (Bal. Account No.) 取得客戶名稱

做法都是:用對應的 Master Table(Bank Account / Customer)去 Get 該筆 Record

一、由 Bank Account No. 取得銀行名稱

使用的 Table

  • Table 270 “Bank Account”
  • 銀行名稱欄位:Name

範例程式碼(Report)

APL
var
    BankAccount: Record "Bank Account";
    BankName: Text[100];
begin
    BankName := '';

    if BankAccount.Get("Bank Account No.") then
        BankName := BankAccount.Name;
end;

常見使用位置

  • OnAfterGetRecord()
  • OnPreReport()
  • 或 Report 的 trigger

二、由 Bal. Account No. 取得客戶名稱

注意:
Bal. Account No. 只是編號,必須搭配 Bal. Account Type 才知道它是「客戶 / 廠商 / G/L」。

程式範例(同時支援 Customer / Vendor / G/L):

APL
var
    Customer: Record Customer;
    Vendor: Record Vendor;
    GLAccount: Record "G/L Account";
    BalAccountName: Text[100];
begin
    BalAccountName := '';

    case "Bal. Account Type" of
        "Bal. Account Type"::Customer:
            if Customer.Get("Bal. Account No.") then
                BalAccountName := Customer.Name;

        "Bal. Account Type"::Vendor:
            if Vendor.Get("Bal. Account No.") then
                BalAccountName := Vendor.Name;

        "Bal. Account Type"::"G/L Account":
            if GLAccount.Get("Bal. Account No.") then
                BalAccountName := GLAccount.Name;
    end;
end;

APL
report 60232 "Check Ledger Entries List"
{
    Caption = 'Check Ledger Entries Report';  // 應收票據帳本分錄清單
    UsageCategory = ReportsAndAnalysis;
    ApplicationArea = All;

    DefaultLayout = RDLC;
    RDLCLayout = 'CheckLedgerEntriesList.rdl'; // 報表的檔案名稱

    // 報表的主要資料集
    dataset
    {

        // 報表的主資料項 (Data Item) 應收票據帳本分錄資料表
        dataitem(CheckLedgerEntries; "Check Ledger Entry")
        {
            // 設定篩選條件 (如果需要,可以在這裡加入過濾器)
            RequestFilterFields =
              "Document No.",
              "Entry No.",
              "Bank Account No.",
              "Posting Date",
              "Bal. Account No.";
            // 排序設定
            DataItemTableView = sorting("Document No.", "Entry No.");

            // 定義要在報表中顯示的欄位
            column(CustomerNo; "Bal. Account No.")
            {
                Caption = '客戶編號';
            }
            column(BankAccountNo; "Bank Account No.")
            {
                Caption = '銀行代號';
            }
            column(BankAccountType; "Bal. Account Type")
            {
                Caption = '銀行類型';
            }
            // ===== 新增:名稱欄位(綁定變數)=====
            column(BankAccountName; BankAccountNameTxt)
            {
                Caption = '銀行名稱';
            }

            column(CustomerName; CustomerNameTxt)
            {
                Caption = '客戶名稱';
            }

						trigger OnAfterGetRecord()
						begin
							// 預設清空
							BankAccountNameTxt := '';
							CustomerNameTxt := '';

							// 1️⃣ 由 Bank Account No. 取得銀行名稱
							if ("Bank Account No." <> '') then
								if BankAccount.Get("Bank Account No.") then
									BankAccountNameTxt := BankAccount.Name;

							// 2️⃣ 由 Bal. Account No. 取得客戶名稱(一定要判斷 Type
							if ("Bal. Account Type" = "Bal. Account Type"::Customer) and
							   ("Bal. Account No." <> '') then
								if Customer.Get("Bal. Account No.") then
									CustomerNameTxt := Customer.Name;
						end;
        }
    }
    // ===== 全域變數 =====
    var
    BankAccount: Record "Bank Account";
    Customer: Record Customer;
    BankAccountNameTxt: Text[100];
    CustomerNameTxt: Text[100];    
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


內容索引