Update Contact plugin Code MS CRM

 public class UpdateContactOnAccountUpdate : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity updatedAccount = (Entity)context.InputParameters["Target"];

                if (updatedAccount.LogicalName == "account" && updatedAccount.Attributes.Contains("your_account_attribute"))
                {
                    // Retrieve the associated contact record
                    Guid contactId = (Guid)updatedAccount.Attributes["your_account_attribute"];
                    Entity contact = service.Retrieve("contact", contactId, new ColumnSet(true));

                    // Update the contact record
                    contact["your_contact_attribute"] = "New Value";
                    service.Update(contact);
                }
            }
        }
    }

Comments

Popular posts from this blog

Power Automate - Update Child record when parent record is updated by using power automate in MS Dynamics CRM

set the active stage in Dynamics 365 when the option set value is "yes"

Create a Task plugin in MS Dynamics CRM