Dynamics 365 Business Central: How to get the current state of the tenant license, and the start and end dates of the license (Codeunit 2300 “Tenant License State”)

Dynamics 365 Business Central

Hi, Readers.
I was asked an interesting question a few days ago, is there any way to check whether the current license of Business Central Tenant is trial via AL? If so, they want to add some restrictions to their extensions.
Yes, it is possible and in this post I would like to talk about how to do it, hope this will help.

This time we need to use codeunit 2300 “Tenant License State”.

This module provides methods for retrieving the current state of the tenant license, and the start and end dates of the license.

For more details: Tenant License State

Use this module to do the following:

  • Get the start date or the end date of the current license.
  • Check whether the license state is evaluation, trial, or suspended.
  • Check whether the trial period has been extended.
  • Check whether the current license is paid, suspended, or warning.

For on-premises versions, you can also use this module to extend the period for a trial license.

Let’s see a simple example, I will use the following methods.

    /// <summary>
    /// Returns the default number of days that the tenant license can be in the current state, passed as a parameter.
    /// </summary>
    /// <param name="TenantLicenseState">The tenant license state.</param>
    /// <returns>The default number of days that the tenant license can be in the current state, passed as a parameter or -1 if a default period is not defined for the state.</returns>
    procedure GetPeriod(TenantLicenseState: Enum "Tenant License State"): Integer
    begin
        exit(TenantLicenseStateImpl.GetPeriod(TenantLicenseState));
    end;

    /// <summary>
    /// Gets the start date for the current license state.
    /// </summary>
    /// <returns>The start date for the current license state or a blank date if no license state is found.</returns>

    procedure GetStartDate(): DateTime
    begin
        exit(TenantLicenseStateImpl.GetStartDate());
    end;

    /// <summary>
    /// Gets the end date for the current license state.
    /// </summary>
    /// <returns>The end date for the current license state or a blank date if no license state is found.</returns>
    procedure GetEndDate(): DateTime
    begin
        exit(TenantLicenseStateImpl.GetEndDate());
    end;

    /// <summary>
    /// Checks whether the current license state is trial.
    /// </summary>
    /// <returns>True if the current license state is trial, otherwise false.</returns>
    procedure IsTrialMode(): Boolean
    begin
        exit(TenantLicenseStateImpl.IsTrialMode());
    end;

    /// <summary>
    /// Checks whether the current license state is paid.
    /// </summary>
    /// <returns>True if the current license state is paid, otherwise false.</returns>
    procedure IsPaidMode(): Boolean
    begin
        exit(TenantLicenseStateImpl.IsPaidMode());
    end;

    /// <summary>
    /// Gets the the current license state.
    /// </summary>
    /// <returns>The the current license state.</returns>
    procedure GetLicenseState(): Enum "Tenant License State"
    begin
        exit(TenantLicenseStateImpl.GetLicenseState());
    end;

PS: Tenant License State (Enum 2301) has the tenant license state types.

For example:

Run in a Trial Tenant.

Test Video:

Run in a Paid Tenant. (Seems to be unable to get the End Date of Paid Tenant, because of no expiration?)

Source Code:

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        TenantLicenseState: Codeunit "Tenant License State";
        TenantLicenseStateEnum: Enum "Tenant License State";
        TenenatLicenseInfoMsg: Label 'Tenenat License State is ''%1''\Period is ''%2''\Start Date is %3\End Date is ''%4''';
        Period: Integer;
        LicenseState: Text[20];
        StartDate: DateTime;
        EndDate: DateTime;
    begin
        LicenseState := Format(TenantLicenseState.GetLicenseState());
        if TenantLicenseState.IsTrialMode() then
            Period := TenantLicenseState.GetPeriod(TenantLicenseStateEnum::Trial);
        if TenantLicenseState.IsPaidMode() then
            Period := TenantLicenseState.GetPeriod(TenantLicenseStateEnum::Paid);
        StartDate := TenantLicenseState.GetStartDate();
        EndDate := TenantLicenseState.GetEndDate();
        Message(TenenatLicenseInfoMsg, LicenseState, Period, StartDate, EndDate);
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL