Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Answered

Auto populating Dimension from Header to Lines AL

Posted on by

Dear All,

I am trying to auto populate the dimensions from Header to line once I select account number on the lines, but nothing is forthcoming.

Both header and line are custom objects.

My code is per below kindly assist.

trigger OnValidate()
var
PHeader: Record Payment;
begin
If "Account Type" = "Account Type"::Vendor then
Begin
PHeader.Reset();
PHeader.SetRange("No.", "Document Account No.");
If PHeader.FindFirst() then
Message('%1', PvHeader."No.");
"Global Dimension 1 Code" := PHeader."Global Dimension 1 Code";
"Global Dimension 2 Code" := PHeader."Global Dimension 2 Code";
"Shortcut Dimension 3 Code" := PHeader."Shortcut Dimension 3 Code";
End;
End

  • Verified answer
    TabrezAjaz Profile Picture
    TabrezAjaz 190 on at
    RE: Auto populating Dimension from Header to Lines AL

    I just wrote basic code that needs to be available in header and line tables:

    Header Table:

    table 50139 "Header Table"
    {
        DataClassification = SystemMetadata;

        fields
        {
            field(1; "Entry No."; Integer)
            {
                DataClassification = SystemMetadata;
            }
            field(2; "Name"; Text[100])
            {
                DataClassification = SystemMetadata;
            }
            field(3; "Shortcut Dimension 1 Code"; Code[20])
            {
                CaptionClass = '1,2,1';
                Caption = 'Shortcut Dimension 1 Code';
                TableRelation = "Dimension Value".Code where(
                    "Global Dimension No." = const(1),
                    Blocked = const(false));

                trigger OnValidate()
                begin
                    ValidateShortcutDimCode(1, "Shortcut Dimension 1 Code");
                end;
            }
            field(4; "Shortcut Dimension 2 Code"; Code[20])
            {
                CaptionClass = '1,2,2';
                Caption = 'Shortcut Dimension 2 Code';
                TableRelation = "Dimension Value".Code where(
                    "Global Dimension No." = const(2),
                    Blocked = const(false));

                trigger OnValidate()
                begin
                    ValidateShortcutDimCode(2, "Shortcut Dimension 2 Code");
                end;
            }
            field(5; "Dimension Set ID"; Integer)
            {
                Caption = 'Dimension Set ID';
                Editable = false;
                TableRelation = "Dimension Set Entry";

                trigger OnLookup()
                begin
                    ShowDimensions();
                    ShowDocDim();
                end;

                trigger OnValidate()
                begin
                    DimensionManagement.UpdateGlobalDimFromDimSetID("Dimension Set ID", "Shortcut Dimension 1 Code", "Shortcut Dimension 2 Code");
                end;
            }
        }
        keys
        {
            key(Key1; "Entry No.")
            {
                Clustered = true;
            }
        }

        procedure ShowDocDim()
        var
            OldDimSetID: Integer;
        begin
            OldDimSetID := "Dimension Set ID";
            "Dimension Set ID" :=
              DimMgt.EditDimensionSet(
                Rec, "Dimension Set ID", StrSubstNo('%1 %2', "Entry No.", Name),
                "Shortcut Dimension 1 Code", "Shortcut Dimension 2 Code");

            if OldDimSetID <> "Dimension Set ID" then begin
                Modify();
                UpdateAllLineDim("Dimension Set ID", OldDimSetID);
            end;
        end;

        trigger OnInsert()
        begin
            ValidateShortcutDimCode(1, "Shortcut Dimension 1 Code");
            ValidateShortcutDimCode(2, "Shortcut Dimension 2 Code");
        end;

        var
            DimensionManagement: Codeunit DimensionManagement;
            DimMgt: Codeunit DimensionManagement;


        procedure ValidateShortcutDimCode(FieldNumber: Integer; var ShortcutDimCode: Code[20])
        var
            OldDimSetID: Integer;
        begin
            OldDimSetID := "Dimension Set ID";
            DimMgt.ValidateShortcutDimValues(FieldNumber, ShortcutDimCode, "Dimension Set ID");


            if OldDimSetID <> "Dimension Set ID" then begin
                Modify();
                UpdateAllLineDim("Dimension Set ID", OldDimSetID);
            end;
        end;

        procedure UpdateAllLineDim(NewParentDimSetID: Integer; OldParentDimSetID: Integer)
        var
            Line: Record "Line Table";
            ATOLink: Record "Assemble-to-Order Link";
            xLine: Record "Line Table";
            NewDimSetID: Integer;
            ShippedReceivedItemLineDimChangeConfirmed: Boolean;
            IsHandled: Boolean;
        begin
            if NewParentDimSetID = OldParentDimSetID then
                exit;

            if not ConfirmUpdateAllLineDim(NewParentDimSetID, OldParentDimSetID) then
                exit;

            Line.Reset();
            Line.SetRange("Entry No.", "Entry No.");
            Line.LockTable();
            if Line.Find('-') then
                repeat
                    NewDimSetID := DimMgt.GetDeltaDimSetID(Line."Dimension Set ID", NewParentDimSetID, OldParentDimSetID);

                    if Line."Dimension Set ID" <> NewDimSetID then begin
                        xLine := Line;
                        Line."Dimension Set ID" := NewDimSetID;

                        DimMgt.UpdateGlobalDimFromDimSetID(
                          Line."Dimension Set ID", Line."Shortcut Dimension 1 Code", Line."Shortcut Dimension 2 Code");

                        Line.Modify();
                    end;
                until Line.Next() = 0;
        end;

        local procedure ConfirmUpdateAllLineDim(NewParentDimSetID: Integer; OldParentDimSetID: Integer) Confirmed: Boolean;
        var
            Text064: Label 'You may have changed a dimension.\\Do you want to update the lines?';
        begin
            Confirmed := Confirm(Text064);
        end;

        procedure ShowDimensions()
        var
            IsHandled: Boolean;
        begin
            IsHandled := false;
            if IsHandled then
                exit;

            "Dimension Set ID" :=
              DimensionManagement.EditDimensionSet(
                Rec, "Dimension Set ID", StrSubstNo('%1 %2', "Entry No.", Name),
                "Shortcut Dimension 1 Code", "Shortcut Dimension 2 Code");
        end;
    }

    Line Table:
    table 50140 "Line Table"
    {
        DataClassification = ToBeClassified;

        fields
        {
            field(1; "Entry No."; Integer)
            {
                DataClassification = ToBeClassified;
            }
            field(2; "Line No."; Integer)
            {
                DataClassification = ToBeClassified;
            }

            field(3; "Shortcut Dimension 1 Code"; Code[20])
            {
                CaptionClass = '1,2,1';
                Caption = 'Shortcut Dimension 1 Code';
                TableRelation = "Dimension Value".Code WHERE("Global Dimension No." = CONST(1),
                                                              Blocked = CONST(false));

                trigger OnValidate()
                begin
                    ValidateShortcutDimCode(1, "Shortcut Dimension 1 Code");
                end;
            }
            field(4; "Shortcut Dimension 2 Code"; Code[20])
            {
                CaptionClass = '1,2,2';
                Caption = 'Shortcut Dimension 2 Code';
                TableRelation = "Dimension Value".Code WHERE("Global Dimension No." = CONST(2),
                                                              Blocked = CONST(false));

                trigger OnValidate()
                begin
                    ValidateShortcutDimCode(2, "Shortcut Dimension 2 Code");
                end;
            }
            field(5; "Dimension Set ID"; Integer)
            {
                Caption = 'Dimension Set ID';
                Editable = false;
                TableRelation = "Dimension Set Entry";

                trigger OnLookup()
                begin
                    ShowDimensions();
                end;

                trigger OnValidate()
                begin
                    DimMgt.UpdateGlobalDimFromDimSetID("Dimension Set ID", "Shortcut Dimension 1 Code", "Shortcut Dimension 2 Code");
                end;
            }
        }

        keys
        {
            key(Key1; "Entry No.", "Line No.")
            {
                Clustered = true;
            }
        }

        procedure ShowDimensions() IsChanged: Boolean
        var
            OldDimSetID: Integer;
            IsHandled: Boolean;
        begin
            OldDimSetID := "Dimension Set ID";
            "Dimension Set ID" :=
              DimMgt.EditDimensionSet("Dimension Set ID", StrSubstNo('%1 %2', "Entry No.", "Line No."));
            DimMgt.UpdateGlobalDimFromDimSetID("Dimension Set ID", "Shortcut Dimension 1 Code", "Shortcut Dimension 2 Code");
        end;

        var
            DimMgt: Codeunit DimensionManagement;

        procedure ValidateShortcutDimCode(FieldNumber: Integer; var ShortcutDimCode: Code[20])
        var
            IsHandled: Boolean;
        begin
            IsHandled := false;

            DimMgt.ValidateShortcutDimValues(FieldNumber, ShortcutDimCode, "Dimension Set ID");
        end;

    }
    On your header card/document page, along your dimension fields, you can also have this action that will open "Dimension Set Entries" page
    actions
        {
            area(processing)
            {
                action(Dimensions)
                {
                    AccessByPermission = TableData Dimension = R;
                    ApplicationArea = Dimensions;
                    Caption = 'Dimensions';
                    PromotedCategory = Process;
                    Promoted = true;
                    PromotedOnly = true;
                    Image = Dimensions;
                    ShortCutKey = 'Alt+D';
                    ToolTip = 'View or edit dimensions, such as area, project, or department, that you can assign to sales and purchase documents to distribute costs and analyze transaction history.';

                    trigger OnAction()
                    begin
                        Rec.ShowDimensions();
                        CurrPage.SaveRecord();
                    end;
                }
            }
        }

    Result: 
    pastedimage1683231746284v3.png
    I have did a lot of work for you today I wish you will like and confirms that my effort works for you.
  • Romryan Profile Picture
    Romryan on at
    RE: Auto populating Dimension from Header to Lines AL

    Tabrez,

    Both are custom tables.

  • TabrezAjaz Profile Picture
    TabrezAjaz 190 on at
    RE: Auto populating Dimension from Header to Lines AL

    I have one question before I suggest something.

    Are the header and lines, base tables, or your custom tables?

  • RE: Auto populating Dimension from Header to Lines AL

    Hi Romryan,

    Kindly verify the answer if it helped.

    Regards,

    Yogender Singh Rana

  • Suggested answer
    Romryan Profile Picture
    Romryan on at
    RE: Auto populating Dimension from Header to Lines AL

    It worked.

    I was using

    PHeader.SetRange("No.", "Document Account No.");

    instead of

    PHeader.SetRange("No.", "No.");

  • Romryan Profile Picture
    Romryan on at
    RE: Auto populating Dimension from Header to Lines AL

    Yogender,

    I have checked the Purchase Line Table and in particularly the function "ValidateShortcutDimCode" but I haven't any relationship between Purchase Header table and the Purchase Line Table.

  • Suggested answer
    RE: Auto populating Dimension from Header to Lines AL

    Hi Romryan,

    Check for the field "Dimension Set ID". I hope the developer would have added this field at the time of development.

    You can pass the value of this field from Header to Line on your specific trigger. And pass the value for Global Dimension fields as per base. You can take reference from the function "ValidateShortcutDimCode" in Purchase Line Table:

    Field - "Shortcut Dimension 1 Code" (Field No.- 40)

    Base Code:

               trigger OnValidate()

               begin

                   ValidateShortcutDimCode(1, "Shortcut Dimension 1 Code");

               end;

    Regards,

    Yogender Singh Rana

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans