建立一個在 Business Central 裡獨立運行的應用程式(CRUD)

一個 完整的 AL 範例,可以在 Business Central 裡獨立運行,不依附於現有頁面,並且支援 CRUD(新增 / 查詢 / 修改 / 刪除)

我們來建立一個 「Basic Table Records」 模組:

  1. 建立一個新的 Table(儲存資料)
  2. 建立一個新的 Page(讓使用者可以操作 CRUD)
  3. 部署到 Sandbox 後,你就能在搜尋列找到這個頁面並使用
  4. 設定權限給用戶

建立 Table: Basic Table

在專案底下新增檔案 BasicTable.al

JavaScript
table 60100 "Basic Table"
{
    DataClassification = ToBeClassified;

    fields
    {
        field(1; "ID"; Integer)
        {
            DataClassification = SystemMetadata;
            AutoIncrement = true; // 主鍵自動編號
        }

        field(2; "Name"; Text[100])
        {
            DataClassification = CustomerContent;
        }

        field(3; "Description"; Text[250])
        {
            DataClassification = CustomerContent;
        }
    }

    keys
    {
        key(PK; "ID")
        {
            Clustered = true;
        }
    }
}

建立 Page: BasicTablePage(CRUD 界面)

JavaScript
page 60100 "Basic Table Page"
{
    PageType = List;
    ApplicationArea = All;
    SourceTable = "Basic Table";
    UsageCategory = Lists; // 讓它可以從搜尋直接找到

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("ID"; Rec."ID") { ApplicationArea = All; Editable = false; }
                field("Name"; Rec."Name") { ApplicationArea = All; }
                field("Description"; Rec."Description") { ApplicationArea = All; }
            }
        }
    }

    actions
    {
        area(processing)
        {
            action(SayTableRecord)
            {
                Caption = 'Say Table Record';
                ApplicationArea = All;

                trigger OnAction()
                var
                    RecCopy: Record "Basic Table";
                begin
                    if Rec.Get(Rec."ID") then begin
                        Message('Hello, %1!', Rec."ID".ToText() + Rec."Name");
                    end;
                end;
            }
        }
    }
}

說明:

  • 這是一個 List Page(清單頁面),可以直接新增、編輯、刪除資料(CRUD)。
  • 透過 UsageCategory = Lists,這個頁面會出現在 Business Central 搜尋功能 (Tell Me) 中。
  • 額外加了一個 SayTableRecord 按鈕,點擊後會顯示當前選擇的紀錄。

設定 launch.json

.vscode/launch.json 加上:

JSON
{
   "version": "0.2.0",
    "configurations": [        
        {
            "name": "Microsoft cloud sandbox",
            "request": "launch",
            "type": "al",
            "environmentType": "Sandbox",
            "environmentName": "Your Environment Name",
            "startupObjectId": 60100,
            "startupObjectType": "Page",
            "breakOnError": "All",
            "launchBrowser": true,
            "enableLongRunningSqlStatements": true,
            "enableSqlInformationDebugger": true,
            "tenant": "Your tenant Code"

        }
    ]
}

設定 app.json

打開根目錄下的 app.json,確認內容大致如下(修改成你的環境資訊):

JSON
{
  "id": "c6b1d2f1-019a-4140-98a5-995f3b1424ae",
  "name": "MS.BasicExercises",
  "publisher": "MingSheng",
  "version": "1.0.0.0",
  "brief": "Customization by MS",
  "description": "Customization by MS",
  "privacyStatement": "",
  "EULA": "",
  "help": "",
  "url": "",
  "logo": "",
  "dependencies": [],
  "screenshots": [],
  "platform": "1.0.0.0",
  "application": "26.0.0.0",
  "idRanges": [
    {
      "from": 60100,
      "to": 60149
    }
  ],
  "resourceExposurePolicy": {
    "allowDebugging": true,
    "allowDownloadingSource": true,
    "includeSourceInSymbolFile": true
  },
  "runtime": "15.2",
  "features": [
    "NoImplicitWith"
  ]
}

可以查詢 extension Management 後, 從頁面中產生 Launch Configurations 檔案

image 2

記得 “application”: “26.0.0.0” 的主要版本要與開發或運行中環境一致,這樣按下 ctl+shift+p 執行 AL: Download symbols 才可以載下正常。

image 3

部署與測試

在 VS Code 按 F5(或 Ctrl+F5

專案會被編譯並部署到 Sandbox

Business Central 瀏覽器自動開啟,會直接進入 Basic Table Page

你可以:

  • 新增紀錄(Create)
  • 瀏覽紀錄(Read)
  • 修改紀錄(Update)
  • 刪除紀錄(Delete)
  • 測試 SayTableRecord 按鈕

這樣你就有了一個完整的 獨立 CRUD 頁面

image 5

權限設定

權限組:

新增一筆權限組: MS BASIC EXERCISES,並點選旁邊的 … 向下切入以記錄 權限組

image 6

控制是否可以 CRUD(新增 / 查詢 / 修改 / 刪除),在於 資料表資料 Basic Table 中的權限,另外要給 頁面: Basic Table Page 執行的權限。

image 7

依使用者權限組

可以先用篩選的方式將權限組:MS BASIC EXERCISES 找出來後,再勾選與用戶即可

image 8

發佈留言

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


內容索引