Dynamics 365 Business Central: How to get Dimension Name, Code Caption, and Filter Caption via AL

Dynamics 365 Business Central

Hi, Readers.
Dimensions are values that categorize entries so you can track and analyze them on documents, such as sales orders. And you can also assign a default dimension for a specific account, such as items. The dimension will be copied to the journal or document when you enter the account number on a line.
Today I would like to talk about how to get dimension Name, Code Caption, and Filter Caption via AL. (Not dimension values)

As you might know, the field caption of Dimensions is changed dynamically in Business Central.
For example, the Global Dimension 1 Code in General Ledger Setup is set to DEPARTMENT.

The caption of Global Dimension 1 on the page will be Department Code.

Department Code is the Code Caption set on the Dimensions page.

Why can caption be changed dynamically?
This is because the CaptionClass property is used in Global Dimensions and Shortcut Dimensions.

PS: If you want to learn how to make the field’s caption change dynamically, you can refer to the following link.
How to change field captions dynamically in Dynamics 365 Business Central

This is a great feature, it makes dimensions suitable for various customers. But, what should we do if we want to get the Dimension information (Name, Code Caption) in development? For instance, we need to display the name of the dimension in the report layout. Or, we need to export data to Excel or other format and display the code caption of the dimension in the header.

There are three common methods, hope this will help. I personally recommend the second method.

1. Get directly from table 348 Dimension

This information is stored in the table 348 Dimension, so you can simply get it directly.

For example:

Source code:

pageextension 50102 "Item Get Dim Info Ext" extends "Item List"
{
    trigger OnOpenPage()
    var
        Dimension: Record Dimension;
        GLSetup: Record "General Ledger Setup";
        GlobalDimension1Name: Text[30];
        GlobalDimension1Caption: Text[80];
        GlobalDimension1FilterCaption: Text[80];
        CaptionMsg: Label 'The Global Dimension 1 Name is ''%1''.\The Global Dimension 1 Caption is ''%2''.\The Global Dimension 1 Filter Caption is ''%3''.';
    begin
        GLSetup.Get();
        Dimension.Get(GLSetup."Global Dimension 1 Code");
        GlobalDimension1Name := Dimension.Name;
        GlobalDimension1Caption := Dimension."Code Caption";
        GlobalDimension1FilterCaption := Dimension."Filter Caption";
        Message(CaptionMsg, GlobalDimension1Name, GlobalDimension1Caption, GlobalDimension1FilterCaption);
    end;
}

2. Use standard functions (GetMLName, GetMLCodeCaption, GetMLFilterCaption) – Recommend

As you might know, we can add translations for the dimensions, so when you try to get dimension information, you also need to consider the current language.

Translated item descriptions are automatically inserted on documents according to the language code.

In table 348 Dimension, there are actually three standard functions that can be used.

For example:

Source Code:

pageextension 50102 "Item Get Dim Info Ext" extends "Item List"
{
    trigger OnOpenPage()
    var
        Dimension: Record Dimension;
        GLSetup: Record "General Ledger Setup";
        GlobalDimension1Name: Text[30];
        GlobalDimension1Caption: Text[80];
        GlobalDimension1FilterCaption: Text[80];
        CaptionMsg: Label 'The Global Dimension 1 Name is ''%1''.\The Global Dimension 1 Caption is ''%2''.\The Global Dimension 1 Filter Caption is ''%3''.';
    begin
        GLSetup.Get();
        Dimension.Get(GLSetup."Global Dimension 1 Code");
        GlobalDimension1Name := Dimension.GetMLName(GlobalLanguage);
        GlobalDimension1Caption := Dimension.GetMLCodeCaption(GlobalLanguage);
        GlobalDimension1FilterCaption := Dimension.GetMLFilterCaption(GlobalLanguage);
        Message(CaptionMsg, GlobalDimension1Name, GlobalDimension1Caption, GlobalDimension1FilterCaption);
    end;
}

Language: English (United States)

Language: Japanese (Japan)

Test Video:

3. Use Record.FieldName Method and Record.FieldCaption Method

You can use Record.FieldName Method and Record.FieldCaption Method to get the name and current caption of a field as a string.

For example:

Source code:

pageextension 50102 "Item Get Dim Info Ext" extends "Item List"
{
    trigger OnOpenPage()
    var
        GlobalDimension1Name: Text[30];
        GlobalDimension1Caption: Text[80];
        GlobalDimension1FilterCaption: Text[80];
        CaptionMsg: Label 'The Global Dimension 1 Name is ''%1''.\The Global Dimension 1 Caption is ''%2''.\The Global Dimension 1 Filter Caption is ''%3''.';
    begin
        GlobalDimension1Name := Rec.FieldName("Global Dimension 1 Code");
        GlobalDimension1Caption := Rec.FieldCaption("Global Dimension 1 Code");
        GlobalDimension1FilterCaption := Rec.FieldCaption("Global Dimension 1 Filter");
        Message(CaptionMsg, GlobalDimension1Name, GlobalDimension1Caption, GlobalDimension1FilterCaption);
    end;
}

Note:
1. For dimension, the Record.FieldName Method does not get the correct name, but only the field name, for example, ‘Global Dimension 1 Code’.

2. Record.FieldCaption Method can also get captions for other languages. This is the same as method 2, but much easier to use.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

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