Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Bulk Contact merge using console application

(0) ShareShare
ReportReport
Posted on by 67

Hi I am trying to use the below code to contact merge ,

but this code gives only for text field how about two options ,optionset values and date values


for example if the master contact record has one field(attribute) with flag yes or no, for this how we do validate for subordinate record and master record and how do we assign that value from subordinate record to master record, please help for the option set values and date values as well.

public void MergeAccounts(Guid masterAccountId, Guid subOrdinateAccountId)
       {
           try
           {
               //Create CRM Connection       
               IOrganizationService crmConnection = GetCRMConnection();
 
               //Create the target for the request.       
               EntityReference target = new EntityReference();
 
               //Id is the GUID of the account that is being merged into.       
               //LogicalName is the type of the entity being merged to, as a string       
               target.Id = masterAccountId;
               target.LogicalName = "account";
 
               //Create the request.       
               MergeRequest merge = new MergeRequest();
 
               // SubordinateId is the GUID of the account merging.   
               merge.SubordinateId = subOrdinateAccountId;
               merge.Target = target;
               merge.PerformParentingChecks = false;
               Entity updateContent = new Entity("account");
               var cols = new ColumnSet(new[] { "primarycontactid", "websiteurl", "telephone1", "fax", "emailaddress1" });
 
               //Get Master Account Primary Contact,Website,Phone,Fax,Email
               var masterAccount = crmConnection.Retrieve("account", masterAccountId, cols);
 
               //Get Subordinate Account Primary Contact,Website,Phone,Fax,Email
               var subOrdinateAccount = crmConnection.Retrieve("account", subOrdinateAccountId, cols);
 
               //If PrimaryContact,Website,Phone,Fax,Email fields data are populated on the Subordinate Account and NOT populated on the Master Account. updated these Subordinate account values to the Master record.                 
               if (!masterAccount.Contains("primarycontactid") && subOrdinateAccount.Contains("primarycontactid"))
                   updateContent.Attributes.Add("primarycontactid", new EntityReference("contact", subOrdinateAccount.GetAttributeValue<EntityReference>("primarycontactid").Id));
 
               if (!masterAccount.Contains("websiteurl") && subOrdinateAccount.Contains("websiteurl"))
                   updateContent.Attributes.Add("websiteurl", subOrdinateAccount.Attributes["websiteurl"]);
 
               if (!masterAccount.Contains("telephone1") && subOrdinateAccount.Contains("telephone1"))
                   updateContent.Attributes.Add("telephone1", subOrdinateAccount.Attributes["telephone1"]);
 
               if (!masterAccount.Contains("fax") && subOrdinateAccount.Contains("fax"))
                   updateContent.Attributes.Add("fax", subOrdinateAccount.Attributes["fax"]);
 
               if (!masterAccount.Contains("emailaddress1") && subOrdinateAccount.Contains("emailaddress1"))
                   updateContent.Attributes.Add("emailaddress1", subOrdinateAccount.Attributes["emailaddress1"]);
 
               merge.UpdateContent = updateContent;
 
               // Execute the request.  
               MergeResponse mergeRes = (MergeResponse)crmConnection.Execute(merge);
           }
           catch (Exception ex)
           {
               //Throw the exception       
               throw ex;
           }

 Kindly help me here 

  • bravoreddy Profile Picture
    bravoreddy 67 on at
    RE: Bulk Contact merge using console application

    Thanks for your reply Naveen.

  • Verified answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Bulk Contact merge using console application

    Hello Bravo,

    You can get other values like:

    OptionSet  --   masterAccount.GetAttributeValue<OptionSetValue>("fieldName").Value  or (OptionSetValue)incidentEntity["fieldName"])

    Datetime --     masterAccount.GetAttributeValue<DateTime>("fieldName");

    Decimal --      masterAccount.GetAttributeValue<Decimal>("fieldName");

    Lookup --       masterAccount.GetAttributeValue<EntityReference>("fieldName").Id

    Bool --            masterAccount.GetAttributeValue<bool>("fieldName");

  • bravoreddy Profile Picture
    bravoreddy 67 on at
    RE: Bulk Contact merge using console application

    Can Someone help me on this ? I haven't receive any reply for this post?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans