Dynamics 365 Business Central: How to get information about the extension that is currently running (ModuleInfo Data Type and NavApp.GetCurrentModuleInfo Method)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to discuss how to get information about the extension that is currently running.

When you develop a new extension version, you must consider the data from the previous version. You have to determine the modifications that must be made to the data to make it compatible with the current version. For example, maybe the new version adds a new field that needs default values set for existing records. Or, the new version adds new tables that must be linked to existing records. To address this type of data handling, you must write upgrade code for the extension version.

Each extension version has a set of properties that contain information about the extension, including AppVersion, DataVersion, Dependencies, Id, Name, and Publisher. This information can be useful when upgrading.

The AppVersion is one of the available properties and its value differs depending on the context of the code being run:

  • Normal operation: AppVersion represents the value of the currently installed extension.
  • Installation code: AppVersion represents the version of the extension you’re trying to install.
  • Upgrade code: AppVersion represents the version of the extension that you’re upgrading to (in other words, the ‘newer’ version).

Another one of the more important properties is the DataVersion property, that represents the value of most recently installed/uninstalled/upgraded version of the extension, meaning that it reflects the most recent version of the data on the system, be that from the currently installed, or a previously uninstalled extension. The DataVersion property value differs depending on the context of the code being run:

  • Normal operation: DataVersion represents the version of the currently installed extension, in which case it’s identical to the AppVersion property.
  • Installation code:
    • Reinstallation (applying the same version): DataVersion represents the version of the extension you’re trying to install (identical to the AppVersion property).
    • New installation: DataVersion represents the value of ‘0.0.0.0’ that’s used to indicate there’s no data.
  • Upgrade code:
    • The version of the extension you’re upgrading from. Either what was last uninstalled, or what is currently installed.

All these properties are encapsulated in a ModuleInfo data type. You can access these properties through the NavApp.GetCurrentModuleInfo methods.

Let’s see some details:

ModuleInfo Data Type:
Represents information about an application consumable from AL.

The following methods are available on instances of the ModuleInfo data type.

Method nameDescription
AppVersion()Gets the version of the specified application’s metadata.
DataVersion()Gets the version of the specified application’s data in the context of a given tenant. This indicates the last version that was installed or successfully upgraded to and will not match the application version if the tenant is in a data upgrade pending state.
Dependencies()Gets the collection of application dependencies.
Id()Gets the ID of the specified application.
Name()Gets the name of the specified application.
Publisher()Gets the publisher of the specified application.

NavApp.GetCurrentModuleInfo Method:

Gets information about the application that contains the AL object that is currently running.

For example: The app.json file in may test extension.

AppVersion() and DataVersion()

Version Data Type:
Represents a version matching the format: Major.Minor.Build.Revision.

The following methods are available on instances of the Version data type.

Method nameDescription
Build()Gets the build number of the version.
Major()Gets the major number of the version.
Minor()Gets the minor number of the version.
Revision()Gets the revision number from the version.
pageextension 50100 CustomerListExt2 extends "Customer List"
{
    trigger OnOpenPage();
    var
        Info: ModuleInfo;
        DataVersionInfo: Label 'The dataversion info:\\The Major Number is ''%1''.\\The Minor Number is ''%2''.\\The Build Number is ''%3''.\\The Revision Number is ''%4''.';
    begin
        NavApp.GetCurrentModuleInfo(Info);
        Message(DataVersionInfo, Info.DataVersion.Major, Info.DataVersion.Minor, Info.DataVersion.Build, Info.DataVersion.Revision);
    end;
}

Dependencies()

ModuleInfo.Dependencies Method:
Gets the collection of application dependencies.

Return Value:
Dependencies  Type: List of [ModuleDependencyInfo] Collection of application dependencies.

ModuleDependencyInfo Data Type:
Provides information about a dependent module.

The following methods are available on instances of the ModuleDependencyInfo data type.

Method nameDescription
Id()Gets the app ID of the specified app.
Name()Gets the name of the specified application.
Publisher()Gets the publisher of the specified application.

Note that by default, each extension you create is automatically added with a dependency extension (Application), so the dependencies you add in app.json start with the second one.

pageextension 50100 CustomerListExt2 extends "Customer List"
{
    trigger OnOpenPage();
    var
        Info: ModuleInfo;
        DependencyInfo01: Label 'The first dependency info:\\The Id is ''%1''.\\The Name is ''%2''.\\The Publisher is ''%3''.';
        DependencyInfo02: Label 'The second dependency info:\\The Id is ''%1''.\\The Name is ''%2''.\\The Publisher is ''%3''.';
    begin
        NavApp.GetCurrentModuleInfo(Info);
        Message(DependencyInfo01, Info.Dependencies.Get(1).Id, Info.Dependencies.Get(1).Name, Info.Dependencies.Get(1).Publisher);
        Message(DependencyInfo02, Info.Dependencies.Get(2).Id, Info.Dependencies.Get(2).Name, Info.Dependencies.Get(2).Publisher);
    end;
}

The first message:

The second message:

Id(), Name() and Publisher()

pageextension 50100 CustomerListExt2 extends "Customer List"
{
    trigger OnOpenPage();
    var
        Info: ModuleInfo;
        CurrentExtInfo: Label 'The current extension info:\\The Id is ''%1''.\\The Name is ''%2''.\\The Publisher is ''%3''.';
    begin
        NavApp.GetCurrentModuleInfo(Info);
        Message(CurrentExtInfo, Info.Id, Info.Name, Info.Publisher);
    end;
}

Okay, let’s summarize.
app.json:

Test code:

Test video:

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL