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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics NAV (Archived)

Events in Business Central

(0) ShareShare
ReportReport
Posted on by 453

Hi 

I want to add some custom code to codeunit 80, and as I see in Business Central we must use events to handle this. 

I have seen many examples but I still have difficulties to understand how we can trigger the customized code to run from a specific place in the code. 

My examle:

This is the standard code in codeunit 80:

LOCAL PostGLAndCustomer(SalesHeader : Record "Sales Header";VAR TempInvoicePostBuffer : TEMPORARY Record "Invoice Post. Buffer";VAR CustLedgEntry : Record "Cust. Ledger Entry")
OnBeforePostGLAndCustomer(SalesHeader,TempInvoicePostBuffer,CustLedgEntry);

WITH SalesHeader DO BEGIN
  // Post sales and VAT to G/L entries from posting buffer
  PostInvoicePostBuffer(SalesHeader,TempInvoicePostBuffer);

  // Post customer entry
  IF GUIALLOWED THEN
    Window.UPDATE(4,1);
  PostCustomerEntry(
    SalesHeader,TotalSalesLine,TotalSalesLineLCY,GenJnlLineDocType,GenJnlLineDocNo,GenJnlLineExtDocNo,SrcCode);

  UpdateSalesHeader(CustLedgEntry);

  // Balancing account
  IF "Bal. Account No." <> '' THEN BEGIN
    IF GUIALLOWED THEN
      Window.UPDATE(5,1);
    PostBalancingEntry(
      SalesHeader,TotalSalesLine,TotalSalesLineLCY,GenJnlLineDocType,GenJnlLineDocNo,GenJnlLineExtDocNo,SrcCode);
  END;
END;



This is my customized code that I want to add:


LOCAL PostGLAndCustomer(SalesHeader : Record "Sales Header";VAR TempInvoicePostBuffer : TEMPORARY Record "Invoice Post. Buffer";VAR CustLedgEntry : Record "Cust. Ledger Entry")
OnBeforePostGLAndCustomer(SalesHeader,TempInvoicePostBuffer,CustLedgEntry);

WITH SalesHeader DO BEGIN
  // Post sales and VAT to G/L entries from posting buffer
  PostInvoicePostBuffer(SalesHeader,TempInvoicePostBuffer);

  // Post customer entry
  IF GUIALLOWED THEN
    Window.UPDATE(4,1);
  PostCustomerEntry(
    SalesHeader,TotalSalesLine,TotalSalesLineLCY,GenJnlLineDocType,GenJnlLineDocNo,GenJnlLineExtDocNo,SrcCode);

  UpdateSalesHeader(CustLedgEntry);

  // Balancing account
  IF "Bal. Account No." <> '' THEN BEGIN
    //My custom code - begin
    Message('This is my test code');
    //My custom code - end   
    PostBalancingEntry(
      SalesHeader,TotalSalesLine,TotalSalesLineLCY,GenJnlLineDocType,GenJnlLineDocNo,GenJnlLineExtDocNo,SrcCode);
  END;  
END;

I have done following until now:
  1. Added an eventsubscriber to the nearest event I found.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnBeforePostGLAndCustomer', '', true, true)]
    local procedure PostGLAndCustomer(SalesHeader: Record "Sales Header"; var TempInvoicePostBuffer: Record "Invoice Post. Buffer"; var CustLedgerEntry: Record "Cust. Ledger Entry")
Begin
//I don't know what to do here to add my customization...

End;



*This post is locked for comments

I have the same question (0)
  • Mehdi26 Profile Picture
    453 on at

    I have accidentally flagged the post as spam. Its not spam :)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans