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 ( Page 2 )

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

AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more
Clean up your copied environments

Clean up your copied environments

One of the most important things every developer should handle is to clean up the environment when the environment (or a company) is copied. Especially if the environment is managed directly by a client and they can create new copies anytime. Similarly, for copied...

read more
7 git commands you should know

7 git commands you should know

For many years, developers in C/AL did not need to know anything about Git or other versioning tools. That has changed with Business Central, or more specifically with AL Language. Today, we will look at the most important (and basic) git commands any developer should...

read more
How to define the source for item reservations?

How to define the source for item reservations?

It's not uncommon to have a customer's request to limit from which source items could be reserved. For example, customers may not want to reserve items from return orders. How can we achieve this goal? It's really simple. All the magic is done in the procedure...

read more
NavigationAction for ErrorInfo data type

NavigationAction for ErrorInfo data type

One more article about ErrorInfo data type. Have you already read my previous posts about ErrorInfo and Collectible Errors? ErrorInfo data type & Collectible Errors | MSDyn365 Business Central - Ing. Tomáš Kapitán (kepty.cz) Collectible Errors?! | MSDyn365...

read more
Error actions for ErrorInfo data type

Error actions for ErrorInfo data type

It is already almost one and a half years since the ErrorInfo data type was introduced (we saw this data type for the first time in BC2021w2). If you do not know what it is and how to use this data type, check my previous posts: ErrorInfo data type & Collectible...

read more
Data types under the microscope: List

Data types under the microscope: List

The List data type in AL language represents an ordered collection of objects that can be accessed by their index. Unlike an Array data type, a List does not have a fixed size and does not need to have its dimension specified when it is declared. The List data type...

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