CRM Integration how to: Customize Status/State for CRM Invoice

by Sep 10, 2023CRM Integration

Home 9 Development 9 CRM Integration 9 CRM Integration how to: Customize Status/State for CRM Invoice

CRM Integration how to: Customize Status/State for CRM Invoice

See all articles in CRM Integration | MSDyn365 Business Central – Tomas Kapitan (kepty.cz) category to get max from the OOTB and custom integration.


I have asked Microsoft to extend the base app to allow partners to customize CRM Invoice statuses. And new publishers are already available in 22.5! Are you interested? Continue reading to find out how to use new publishers.

How the CRM Invoice Status/State is updated

CRM Invoice Status/State fields are defined in Codeunit 5342 “CRM Synch. Helper”. There are actually two different procedures that handle those fields – UpdateCRMInvoiceStatusFromEntry(…) and CancelCRMInvoice(…).

The CancelCRMInvoice(…) is used when the Invoice is cancelled (if the Sales Invoice Header has Canceled = true), and the UpdateCRMInvoiceStatusFromEntry(…) is used for all other invoices. That means if you want to change the OOTB functionality for active (not cancelled) invoices only, you don’t need to care about CancelCRMInvoice(…); however, if you want to have a completely different mapping, you need to subscribe to publishers called in both procedures.

Both procedures have the same design. First, the new status is determined and stored in the temporary NewCRMInvoice variable, and if the value is different from the current value, it’s updated later from the NewCRMInvoice.

How to customize the CRM Invoice Status/State

To change the Status/State logic (except for the Cancelled Invoices), you need to subscribe to the OnBeforeCalculateActualStatusCode(…) publisher. This publisher is called from the CalculateActualStatusCode(…) procedure and has three parameters – The original Customer Ledger Entry, the CRM Invoice record and the IsHandle variable. To specify a new state/status, you need to update the CMR Invoice variable state/status fields.

How to update additional fields together with the status

You can also update additional fields when the status is changed. This could be used (for example) to update the remaining amount of the invoice (if you have such a custom field added to the CRM).

To do this, you need to use 4 publishers:

  • local procedure OnUpdateCRMInvoiceStatusFromEntryOnBeforeCheckFieldsChanged(var CRMInvoice: Record “CRM Invoice”; var NewCRMInvoice: Record “CRM Invoice”; CustLedgerEntry: Record “Cust. Ledger Entry”; var ChangeNeeded: Boolean)
  • local procedure OnUpdateCRMInvoiceStatusFromEntryOnBeforeModify(var CRMInvoice: Record “CRM Invoice”; var NewCRMInvoice: Record “CRM Invoice”; CustLedgerEntry: Record “Cust. Ledger Entry”)
  • local procedure OnCancelCRMInvoiceOnBeforeCheckFieldsChanged(var CRMInvoice: Record “CRM Invoice”; var NewCRMInvoice: Record “CRM Invoice”; var ChangeNeeded: Boolean)
  • local procedure OnCancelCRMInvoiceOnBeforeModifyCRMInvoice(var CRMInvoice: Record “CRM Invoice”; var NewCRMInvoice: Record “CRM Invoice”)

Two publishers are for cancelled invoices, and the other two are for all other invoices.

First, you need to subscribe to XXXXOnBeforeCheckFieldsChanged(…) and return in ChangeNeeded true if the record should be modified and if to follow the standard pattern, update your custom field in the temporary variable.

Secondly, you have to subscribe to XXXXOnBeforeModifyCRMInvoice(…) and copy the value for your custom field from the temporary record to the permanent one.

codeunit 5342 "CRM Synch. Helper"
{
    [IntegrationEvent(false, false)]
    local procedure OnUpdateCRMInvoiceStatusFromEntryOnBeforeModify(var CRMInvoice: Record "CRM Invoice"; var NewCRMInvoice: Record "CRM Invoice"; CustLedgerEntry: Record "Cust. Ledger Entry")
    begin
    end;

    [IntegrationEvent(false, false)]
    local procedure OnUpdateCRMInvoiceStatusFromEntryOnBeforeCheckFieldsChanged(var CRMInvoice: Record "CRM Invoice"; var NewCRMInvoice: Record "CRM Invoice"; CustLedgerEntry: Record "Cust. Ledger Entry"; var ChangeNeeded: Boolean)
    begin
    end;

    [IntegrationEvent(false, false)]
    local procedure OnBeforeCalculateActualStatusCode(CustLedgerEntry: Record "Cust. Ledger Entry"; var CRMInvoice: Record "CRM Invoice"; var IsHandled: Boolean)
    begin
    end;

    [IntegrationEvent(false, false)]
    local procedure OnCancelCRMInvoiceOnBeforeModifyCRMInvoice(var CRMInvoice: Record "CRM Invoice"; var NewCRMInvoice: Record "CRM Invoice")
    begin
    end;

    [IntegrationEvent(false, false)]
    local procedure OnCancelCRMInvoiceOnBeforeCheckFieldsChanged(var CRMInvoice: Record "CRM Invoice"; var NewCRMInvoice: Record "CRM Invoice"; var ChangeNeeded: Boolean)
    begin
    end;
}

Recent Articles from the category

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here