Synchronizing countries from D365 Sales to D365 Business Central

by Nov 24, 2023AL Language, CRM Integration

Home 9 Development 9 AL Language 9 Synchronizing countries from D365 Sales to D365 Business Central

Surprisingly, the out-of-the-box integration between Microsoft Dynamics 365 Sales and Microsoft Dynamics 365 Business Central does not synchronize countries. Why? Because it could be hard to maintain this synchronization as, by default, the country field in D365 Sales is just a plain text field (and, as you know, in Business Central, we have a standalone Country/Region table).

The main problem here is not the synchronization itself but possible data inconsistency (as users in D365 Sales can type any value in the country field and the synchronization won’t be able to figure out the correct country in Business Central).

However, you can customize D365 and limit possible values or add some form of validation. This can be done relatively quickly, and once you have such validation in place, you can add support for the country synchronization to the standard connector. How? Continue reading.

We probably want to add custom data transfer process to allow custom country mapping. For this purpose, we can use one of the core events in Codeunit 5336 Integration Record Synch. – OnTransferFieldData.

codeunit 5336 "Integration Record Synch."
{
    ...

    [IntegrationEvent(false, false)]
    local procedure OnTransferFieldData(SourceFieldRef: FieldRef; DestinationFieldRef: FieldRef; var NewValue: Variant; var IsValueFound: Boolean; var NeedsConversion: Boolean)
    begin
    end;
}

Once we know the publisher, we can easily create a subscriber. We need 4 parameters:

  • SourceFieldRef: FieldRef = to check whether the field we got is the one we want to use for the data transfer transformation
  • DestinationFieldRef: FieldRef = to check whether the field we got is the one we want to use for the data transfer transformation
  • NewValue: Variant = to return the new value
  • IsValueFound: Boolean = to indicate whether we found the new value or we want to run the standard process
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Integration Record Synch.", 'OnTransferFieldData', '', false, false)]
procedure SetCountryInBCFromCRM(SourceFieldRef: FieldRef; DestinationFieldRef: FieldRef; var NewValue: Variant; var IsValueFound: Boolean)
begin
    // CHECK IF SOURCE AND DESTINATION FIELDS ARE COUNTRY FIELDS
    IsValueFound := FindCountryRegionCodeFromCRMCountry(SourceFieldRef.Value(), NewValue);
end;

And how are we mapping countries? We are following the basic logic – first, we try to find the CRM country in the Country/Region table in the Code field, in the Name field (exact or partial match), and if we do not find the corresponding value, we have a custom mapping table that we are using for countries that (for example) we want to map to another country, or if the country has multiple names)

procedure FindCountryRegionCodeFromCRMCountry(CRMCountryName: Text; var BCCountryCode: Code[10]): Boolean
begin
    if CRMCountryName = '' then
        exit(false);

    CountryRegion.SetRange(Code, CopyStr(CRMCountryName, 1, MaxStrLen(CountryRegion.Code)));
    // IF FOUND, RETURN FOUND COUNTRY

    CountryRegion.SetRange(Name, CRMCountryName);
    // IF FOUND, RETURN FOUND COUNTRY

    CountryRegion.SetFilter(Name, '@%1', CRMCountryName);
    // IF FOUND, RETURN FOUND COUNTRY

    CRMCountryMapping.SetRange("CRM Country Name", CRMCountryName);
    // IF FOUND, RETURN FOUND COUNTRY

    CRMCountryMapping.SetFilter("CRM Country Name", '@%1', CRMCountryName);
    // IF FOUND, RETURN FOUND COUNTRY

    // THROW AN ERROR IF NOT FOUND
end;

Recent Articles from the category

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

read more
Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more

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