web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

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

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

(1) ShareShare
ReportReport
Posted on by 10
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;
I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,307 Super User 2025 Season 2 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
  • KJ-03062255-0 Profile Picture
    10 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;      
         
       
       
       
    }  

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,377

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 2,696 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,512 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans