Thursday, 8 June 2017

BULK UPDATE OF THE PRIMARY CONTACT'S COMPANY NAMES IN ALL CONTACTS IN DYNAMICS 365

If you want to bulk update the primary contacts "company name" field value in all the contacts records.

Then the below code is helpful:

public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context =
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Get a reference to the Organization service.
            IOrganizationServiceFactory factory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            Entity entity;
            if (context.InputParameters != null)
            {
                entity = (Entity)context.InputParameters["Target"];

                string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
              <entity name='account'>
                <attribute name='name' />
                <attribute name='primarycontactid' />
                <attribute name='telephone1' />
                <attribute name='accountnumber' />
                <attribute name='accountid' />
                <order attribute='name' descending='false' />
                <filter type='and'>
                  <condition attribute='primarycontactid' operator='not-null' />
                </filter>
                <link-entity name='contact' from='contactid' to='primarycontactid' alias='PrimaryContact'>
                  <attribute name='parentcustomerid'  />

                    <attribute name='contactid' alias='contact'/>
                  <filter type='and'>
                    <condition attribute='parentcustomerid' operator='null' />
                  </filter>
                </link-entity>
              </entity>
            </fetch>";
           
                EntityCollection entityCollection = service.RetrieveMultiple(new FetchExpression(fetchXML));
                for (int i = 0; i < entityCollection.Entities.Count; i++)
                {
                 
                        Entity contact = new Entity("contact");
                        contact.Attributes["parentcustomerid"] = new EntityReference(entityCollection.Entities[i].LogicalName, entityCollection.Entities[i].Id);
                        contact.Attributes["contactid"] = entityCollection.Entities[i].GetAttributeValue<AliasedValue>("PrimaryContact.contactid").Value;
                        service.Update(contact);
                 
                   
                }
            }
            else
            {
                return;
            }
        }
========================================================================

Message : Update
Entity: Account
Attribute: email
Post-operation, Asynchronous,


I hope this helps:
Happy CRM ing:-)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.