Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

Events in Business Central

(0) ShareShare
ReportReport
Posted on by 445

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

  • Mehdi26 Profile Picture
    Mehdi26 445 on at
    RE: Events in Business Central

    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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans