Part 4 - Reconciling More than One Match - Merging Accounts
So far, you have seen how to import new records and update existing records, if there is a match. But what happens when there is more than one match? Perhaps someone entered a duplicate account number in your system and it wasn't caught by other means. Just as you are able to merge records in the CRM interface, you can programmatically merge them in your code.The question we are asking is, when you check an account number (from your remote system) against the CRM database, does it return more than one record? You don't want to update only the first one, because if the account number is the unique identifier in your remote system and there are two (or more) in CRM, your data will be inaccurate. You want to be able to update the Account and merge the duplicates on the fly. Here's how that's done.
Find Matched Records
The first thing you will need to check for is if your query matched more than one record. Remember, we checked to see if it existed if "i = 0" or if "i = 1". Now, if the service returns more than one, we will consider the first record the one that remains active, and merge the others to it. We'll need to set up variables to hold information on the target and subordinate account, as well as a marker letting us know that we already have our master record.Open the Visual Studio project and find the end of the "if" statement. Add an "else if" to check the condition where i > 1. Add a boolean variable called bDoneOnce and set it to false. Add a variable of type "Account" and call it aMergeAct. Set it to the default value. Next, add a LINQ query to retrieve the IDs for the matching Accounts. Here's what the code should look like so far.
Figure 22 |
Update the First Match
Now you need to loop through the result set. First, you need to create a MergeRequest (to do the work) and EntityReference (for the subordinate account). Then you will check the bDoneOnce variable to see if you already have a target account. If not, retrieve the account and update the fields. Set the bDoneOnce variable to True. Additional matches will be merged to the this Account.Figure 23 |
Now, create an "else" default condition. Assign the next account to the sMergeAct variable. You need to retrieve the Account using the service proxy Retrieve method. Pass it the entity name, ID of the Account, and the columns you want to retrieve. This is the subordinate record. Set up the target account information. Assign it the AccountId that you retrieved in the first part of this section, and the entity name.
Set Up the Merge Function
All you have left to do is set up the merge function. The subordinate id is the one from the aMergeAct that you retrieved. The target attribute is the target you set up. UpdateContent refers to the act that you are updating (from the first part of the code). Then call the Execute method.Figure 24 |
Happy Coding!
Related Links:
Part 1 - Getting Started
Part 2 - Beginning the Import Code
Part 3 - Finishing the Import/Update
I really want to thank the author for such a nice blog that helped me to understand why it is important. Import Data Philippines
ReplyDelete