Dynamics 365 Business Central mini tips: How to get Tenant GUID in AL (Not Tenant ID)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to get Tenant GUID in AL.

As you might know, we can easily use TenantId() Method to get the the tenant ID.

Database.TenantId() Method: Gets the ID of the tenant that has started the current session. Use this method when your code must be specific about which tenant database to access in a multitenant deployment.

Let’s look at a very simple example.

Tenant Id is msasia4477t86002005.

But in the MS Docs, The Business Central Administration Center.

The tenant ID is shown in the Help and Support page in your Business Central.

Choose Settings -> Help & Support.

You can see Azure AD Tenant.
But the tenant ID is different from the ID get by TenantId() Method in AL. This is a GUID.

Azure AD tenant: d8f36038-1f93-4543-affc-5dc92b6ee871

Let’s go back to the topic, how to get this Tenant GUID in AL?
In fact, this tenant GUID exists in the WebClient URL.

So we can use System.GetUrl() Method.
System.GetUrl(ClientType [, String] [, ObjectType] [, Integer] [, Record] [, Boolean]) Method: Generates a URL for the specified client target that is based on the configuration of the server instance. If the code runs in a multitenant deployment architecture, the generated URL will automatically apply to the tenant ID of the current user.

For example:

Next, extract the GUID separately from the URL. There are many ways to do this. I recommend using List Data Type if the format of the URL does not change.

Tenant Id is ‘msasia4477t86002005’.
Tenant Guid is ‘d8f36038-1f93-4543-affc-5dc92b6ee871’.

Test Source Code:

pageextension 50103 CustomerExt extends "Customer List"
{
    trigger OnOpenPage()
    var
        Msg: Label 'Tenant Id is ''%1''.\Tenant Guid is ''%2''.';
        BCURLList: List of [Text];
        TenantIdTxt: Text;
        TenantGuidTxt: Text;
    begin
        TenantIdTxt := TenantId();
        BCURLList := GetUrl(ClientType::Web).Split('/');
        TenantGuidTxt := BCURLList.Get(4);
        Message(Msg, TenantIdTxt, TenantGuidTxt);
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました