Thursday, June 19, 2014

Part 4 - Creating a Custom Data Import with Microsoft CRM SDK

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
Again, you can write a function to call the field and owner assignments, since we are repeating this for the adding, updating and merging of Accounts.  

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
That's really all there is to it.  Keep in mind that there are many different ways to leverage the SDK and that this is merely one of them.  There may be more posts in the future regarding different data types and batching for quicker imports.  Leave me a reply and let me know if you are having issues or questions.  

Happy Coding!

Related Links:
Part 1 - Getting Started 
Part 2 - Beginning the Import Code 
Part 3 - Finishing the Import/Update
 

 

4 comments:

  1. "codeaze is the reliable and the trustworthy name in the world of web development and have helped a number of brands in designing and developing their websites.
    Address: Asia Pacific Trade Center, Rashid Minhas Rd, Karachi
    Email: info@codeaze.org
    Phone: 0316 2586964" Codeaze

    ReplyDelete
  2. 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
  3. Thanks for sharing this information Freyr Energy was founded on the principles of making solar energy affordable and accessible for everyone. In order to make adoption of solar energy a reality at the grass-root level, we have identified that consumer awareness, affordability and accessibility play an integral role. With our innovative platform, SunPro+, our extensive channel-partner network and our efficient service we ensure that these three factors are addressed to make sure your venture into solar energy is hassle-free. Best solar company in Hyderabad-freyr

    ReplyDelete
  4. Our Informatica Developer Training will provide you to learn the performance of ETL tasks with live classes. ITGuru Informatica Online Training also include projects
    informatica axon training| informatica developer course

    ReplyDelete