Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

AL code - Assign Dimensions Sales header OnAfterInsertEvent

(1) ShareShare
ReportReport
Posted on by 2,659
Hi All
I'm trying to insert sales header dimensions after the insert event of the table, Event sub not working for the below code  . However when triggering from a page action code works perfectly. The issue is with the insert event as first it inserts the value and then a system process removes it so my guess is something else runs after the modify code. Note: default dimensions do not work in this scenario and hard code only for testing
[EventSubscriber(ObjectType::Table, Database::"Sales Header", OnAfterInsertEvent, '', false, false)]
 
    local procedure OnafterInsertEvent(var Rec: Record "Sales Header")
 
    var
        dims: Record "Dimension Set Entry" temporary;
        DimMgt: Codeunit DimensionManagement;
        Dimval2: Code[20];
 
    begin
 
        Dimval2 := 'SALES';
 
        if Rec.IsTemporary then
            Exit;
 
        Message('OnAfterInsertEvent1');//debug
 
        dims.init();
        dims.validate("Dimension Code", 'DEPARTMENT');
        dims.validate("Dimension Value Code", 'SALES');
        Dims.Insert();
 
        UpdateDimSetOnSalesHeader(Rec, Dims);
        Rec.Modify()
 
    end;
 
    procedure UpdateDimSetOnSalesHeader(var SH: Record "Sales Header"; var ToAddDims: Record "Dimension Set Entry" temporary)
    var
        DimMgt: Codeunit DimensionManagement;
        NewDimSet: Record "Dimension Set Entry" temporary;
 
    begin
        DimMgt.GetDimensionSet(NewDimSet, SH."Dimension Set ID");
        if ToAddDims.FindSet() then
            repeat
                if NewDimSet.Get(SH."Dimension Set ID", ToAddDims."Dimension Code") then begin
                    NewDimSet.validate("Dimension Value Code", ToAddDims."Dimension Value Code");
                    NewDimSet.Modify();
                end else begin
                    NewDimSet := ToAddDims;
                    NewDimSet."Dimension Set ID" := SH."Dimension Set ID";
                    NewDimSet.Insert();
                end;
            until ToAddDims.Next() = 0;
        SH."Dimension Set ID" := DimMgt.GetDimensionSetID(NewDimSet);
        DimMgt.UpdateGlobalDimFromDimSetID(SH."Dimension Set ID", SH."Shortcut Dimension 1 Code", SH."Shortcut Dimension 2 Code");
 
    end;
  • Verified answer
    Samantha73 Profile Picture
    2,659 on at
    AL code - Assign Dimensions Sales header OnAfterInsertEvent
    Thanks all - copied the code to a new Sandbox and it worked so there is some other customisations updating the field after modify event
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    9,299 Super User 2025 Season 1 on at
    AL code - Assign Dimensions Sales header OnAfterInsertEvent
    Que: The issue is with the insert event as first it inserts the value and then a system process removes it so my guess is something else runs after the modify code.
     
    I have run the same code, and nothing is modifying or overwriting. Even after posting the invoice, the dimensions are retained correctly. Please clarify if you are facing this issue in a specific scenario or setup, as it doesn't seem to happen in a general environment.
     
     
  • Yi Yong Profile Picture
    1,893 Super User 2025 Season 1 on at
    AL code - Assign Dimensions Sales header OnAfterInsertEvent
    Hello,
     
    I copied the entire code and deployed it to a new sandbox without any customization and it worked fine.
     
    Was the DEPARTMENT field updated or removed from your encounter?
     
    Can you confirm it was not after a change of field? 
  • gdrenteria Profile Picture
    15,252 Most Valuable Professional on at
    AL code - Assign Dimensions Sales header OnAfterInsertEvent
    hi
    Is it possible that after inserting the values, they are updated when, for example, the dimensions of the client or other data from the header are brought? Which dimensions should be valid, those you enter by code or those obtained by the standard code?
    Best
    GR

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,865 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans