Skip to main content
Business Central forum
Suggested answer

AL code not updating a field as I would expect it to

editSubscribe (0) ShareShare
ReportReport
Posted on by 755
I have a codeunit I created that when a Purchase document is /posted/, I want to have the posting description update based on the /Description/ in the first line of the Purchase Lines.  Everything seems to be correct on this, but it will not update the actual field. In the below code, I have the /Posting Description/ message twice, once before I try and change the value, and once after I set it, and the message is correct in both cases, (i.e. Order POXXXXX the first time, then /Utilities/ the second), but the field itself is not being updated. I have switched posting description to other fields (i.e. Vendor Shipment Number), but it does the same thing. What am I missing to have it actually update the field for me?
 
 [EventSubscriber(ObjectType::Table, Database::/Purchase Header/, 'OnBeforeSendToPosting', '', true, true)]
 
    local procedure OnBeforeSendToPosting(var PurchaseHeader: Record /Purchase Header/)
    var
     PurLine: Record /Purchase Line/;
 
    begin
 
            PurLine.SetRange(/Document No./, PurchaseHeader./No./);
            PurLine.FindFirst();
            Message(PurchaseHeader./Posting Description/);
            PurchaseHeader./Posting Description/ := PurLine.Description;
           
            Message(PurchaseHeader./Posting Description/);
     end;
Categories:
  • Community member Profile Picture
    Community member 755 on at
    AL code not updating a field as I would expect it to
    I added PurchaseHeader.Modify(true); but it still did the same thing, I then added commit(); to see if that made a difference, but it still does the same thing.
     
    codeunit 50302 UpdatePurchaseHeader
    {
       
     
     
     
        [EventSubscriber(ObjectType::Table, Database::"Purchase Header", 'OnBeforeSendToPosting', '', true, true)]
     
        local procedure OnBeforeSendToPosting(var PurchaseHeader: Record "Purchase Header")
       
        var
         PurLine: Record "Purchase Line";
         
     
        begin
              PurchaseHeader.Modify(true);
                PurLine.SetRange("Document No.", PurchaseHeader."No.");
                PurLine.FindFirst();
                Message(PurchaseHeader."Posting Description");
               
                PurchaseHeader."Posting Description" := PurLine.Description;
                Commit();
                Message(PurchaseHeader."Posting Description");
             
             end;      
         
       
       
       
    }  
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 59,766 Super User on at
    AL code not updating a field as I would expect it to
    Hi, add a PurchaseHeader.Modify(true)?
     
    Hope this helps.
    Thanks.
    ZHU

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Sundar…

Industry experts answer burning questions directly from our amazing Community…

Enabling Copilot Case and Conversation…

Agents can easily recap an ongoing chat, transcribe a voice conversation…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 282,978 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,562 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans