Customize ฟังก์ชั่น Kill Session บน Dynamics 365 Business Central

Spread the love

สวัสดีครับ สำหรับบทความในวันนี้จะเป็นเรื่องเกี่ยวกับการทำ Customization ให้กับโปรแกรม Microsoft Dynamics 365 Business Central เพื่อเพิ่ม Feature การใช้งานให้สามารถจัดการกับ session ที่ออนไลน์อยู่ในระบบได้ ด้วย AL Extension

stop-session-business-central-1

บทความนี้ประกอบไปด้วย

Business Value

ข้อดีของการมีฟีเจอร์ Kill Session นั้นจะช่วยให้คุณสามารถ หยุดการใช้งาน (Stop Session) ของ User ที่ค้างอยู่ในระบบออกไปได้ หรือบางครั้งก็เจอข้อความว่า “another user has modified” หรือ “a table is locked by another user” คุณก็สามารถจัดการได้ด้วยการ Kill Session โดยไม่ต้องเสียเวลารอจน table หาย lock

Feature นี้จะเป็นการจัดการกับ table ที่ชื่อว่า “Active Session” โดยคุณสามารถดูคน session ที่กำลัง active ได้จาก URL นี้

https://businesscentral.dynamics.com/?table=2000000110

หรือถ้าเป็นแบบ on-premise

http://localhost:8080/BC160/?table=2000000110

เมื่อเปิดขึ้นมาคุณก็จะเห็นว่ามีใครกำลังออนไลน์อยู่

stop-session-business-central-2

ขั้นต่อไป เราจะมาเพิ่มปุ่มเพื่อทำการ Kill Session หรือ เตะ User ออกจากระบบกัน

บทความที่คุณอาจสนใจ

คู่มือ ดาวน์โหลด และ ติดตั้ง โปรแกรม Microsoft Dynamics 365 Business Central version BC 16
ขั้นตอนการติดตั้ง AL Language บน Visual Studio Code สำหรับ Microsoft Dynamics 365 Business Central

Preparing Extension

เริ่มต้นด้วยด้วยการเปิดโปรแกรม Visual Studio Code ขึ้นมาก่อนเลยครับ

stop-session-business-central-3

จากนั้นกด Ctrl + Shift + P แล้วเลิก AL:Go เพื่อทำการ New Project ใหม่

stop-session-business-central-4

ตรงนี้ผมจะขอตั้งชื่อ Project ว่า KillSessions

stop-session-business-central-5

เซ็ตอัพไฟล์ launch.json

stop-session-business-central-6

เซ็ตอัพไฟล์ app.json

stop-session-business-central-7

เมื่อไฟล์ lanuch.json และ app.json เรียบร้อยแล้วก็กด Ctrl + Shift + P แล้วทำการ Download Symbols ได้เลย

stop-session-business-central-8

หากขึ้นข้อความว่า “Symbol have been downloaded” ก็เป็นอันเรียบร้อยสำหรับขึ้นตอนการเตรียม Project ใน Step ต่อไปเราก็จะเริ่มเขียน Code กันแล้ว

stop-session-business-central-9

บทความที่คุณอาจสนใจ

เริ่มต้นสร้าง Extension แรกบน MS Dynamics 365 BC ตอนที่ 1 – New Table
Check List เมื่อไม่สามารถ Download Symbols ให้กับ AL Extension ได้

Customization by AL Extension

เริ่มต้นจากสร้างไฟล์ al ขึ้นมาหนึ่งตัว ตั้งชื่อว่า Pag50110.ActiveSessionList.al (ให้ตรวจสอบก่อนว่าเลข Object ID ถูกใช้งานไปแล้วหรือยัง)

stop-session-business-central-10

จากนั้นใช้ Snippet เพื่อ Generate โครงสร้างของ List Page ขึ้นมา

stop-session-business-central-11

จากได้โครงสร้างของ Page List ตามนี้

stop-session-business-central-12

จากนั้นให้เขียน code ตามรูปด้านล่างได้เลย

layout (part 1)

stop-session-business-central-13

layout (part 2)

stop-session-business-central-14

action

stop-session-business-central-15

ในส่วนของ layout นั่นจะเป็นการเพิ่ม fields ที่จำเป็นในการดู data และในส่วนของ action นั้นจะเป็นการสร้างปุ่ม เพื่อ kill session หรือหยุดการทำงานของ user ที่ต้องการให้ออกจากระบบนั่นเอง

เมื่อเขียน Code เสร็จแล้วก็ Install และ Publish ตัว AL Extension ของเราขึ้นระบบได้เลย (ctrl + f5)

บทความที่คุณอาจสนใจ

วิธีพิมพ์ QR Code ใน Report บน Dynamics 365 BC ด้วย .NET Framework
ขั้นตอนการสร้าง .NET DLL (Library Class) ไว้ใช้งานเอง บน Dynamics 365 Business Central

Testing Kill Session Feature

หลังจากที่เรา install และ publish ขั้นระบบแล้วก็มาที่หน้า role center ได้เลย

stop-session-business-central-16

จากนั้นให้ Search คำว่า Active Session List ตามชื่อ page ที่เราตั้งได้เลย

stop-session-business-central-17

ก็จะได้หน้าตาของ Page ประมาณนี้

stop-session-business-central-18

โดยจะมีปุ่ม Kill Session ขึ้นมาสำหรับ หยุดการใช้งานของ User นั้น

เมื่อคลิกที่ปุ่ม Kill Session จะมี Confirm ถามเพื่อยืนยันก่อน

stop-session-business-central-19

สำหรับใครที่ขี้เกียจเขียน code เองก็ copy ตัว source code จากด้านล่างนี้ได้เลยครับ

แล้วเจอกันใหม่ในบทความหน้า สวัสดีครับ

page 50110 "Active Session List"
{
    PageType = List;
    ApplicationArea = All;
    UsageCategory = Lists;
    SourceTable = "Active Session";

    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field("User SID"; "User SID")
                {
                    ApplicationArea = All;
                }
                field("Server Instance ID"; "Server Instance ID")
                {
                    ApplicationArea = All;
                }
                field("Session ID"; "Session ID")
                {
                    ApplicationArea = All;
                }
                field("Server Instance Name"; "Server Instance Name")
                {
                    ApplicationArea = All;
                }
                field("Server Computer Name"; "Server Computer Name")
                {
                    ApplicationArea = All;
                }
                field("User ID"; "User ID")
                {
                    ApplicationArea = All;
                }
                field("Client Type"; "Client Type")
                {
                    ApplicationArea = All;
                }
                field("Client Computer Name"; "Client Computer Name")
                {
                    ApplicationArea = All;
                }
                field("Login Datetime"; "Login Datetime")
                {
                    ApplicationArea = All;
                }
                field("Database Name"; "Database Name")
                {
                    ApplicationArea = All;
                }
                field("Session Unique ID"; "Session Unique ID")
                {
                    ApplicationArea = All;
                }
            }
        }
    }

    actions
    {
        area(Processing)
        {
            group(Function)
            {
                action("Kill Session")
                {
                    ApplicationArea = All;
                    Image = Cancel;
                    Promoted = true;
                    PromotedIsBig = true;

                    trigger OnAction();
                    begin
                        if Confirm('Do you want to kill this session?', true) then
                            StopSession("Session ID");
                    end;
                }
            }
        }
    }
}

ERP Consultant ที่อยากแชร์ความรู้เกี่ยวกับการพัฒนาโปรแกรม Microsoft Dynamics NAV / 365 Business Central ทั้งทางด้าน Business และ Technical ให้กับเหล่านักศึกษา, ผู้ที่เริ่มต้นอาชีพนี้ หรือต้องการที่จะเพิ่มเติมความรู้ด้าน ERP ในรูปแบบ blog ที่เป็นภาษาไทย Contact: amaddev90@gmail.com

2 thoughts on “Customize ฟังก์ชั่น Kill Session บน Dynamics 365 Business Central”

  1. ถ้าทำตามแบบนี้ เราสามารถลบ user ที่ลบ ไม่ได้ ใช่ไหมครับ

    1. ใช่ครับ สามารถ kill session ของ user ที่ค้างในระบบได้ครับ

Leave a Reply

Your email address will not be published. Required fields are marked *