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:
- 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