CRM 插件:如何在更新帐户地址时更新关联联系人的地址

CRM Plugin: how to update associated contacts' addresses when you update account address

我正在使用 Microsoft Dynamics CRM 2016 本地版本。

我有一个 "account" entity 关联了 "contacts". 当更新当前 "account" entity's "address" 时,我希望所有关联的联系人地址都更新为该地址。

我想在更新地址时在 "account" 实体上运行的插件中执行此操作。当您这样做时,所有关联联系人的地址都会更新为该地址。

我对此进行了一些搜索,但没有任何内容显示地址正在更新。例如,示例通常显示正在更新的 phone 号码。使地址更复杂的是地址存储在地址实体中,所以我想我必须得到某种 addressId primary key 并将其放入每个 associated contacts address FK field. 我不知道如何找到任何类似的例子。

Does anyone have a snippet of code that will go into the plugin?

[注意我打算把它放在插件代码的 public void Execute(IServiceProvider serviceProvider) 方法中。]

更新时不需要联系人地址的 ID。联系人实体中已包含两种类型的地址。它们通常用于 postal 和访问地址。地址字段名称以 address1_address2_ 为前缀。因此,只需在联系人实体上像这样设置这些字段:

contact["address1_line1"] = "John Doe";

插件可能如下所示:

public class AccountPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        var orgService = factory.CreateOrganizationService(null);
        var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        Entity account = context.PostEntityImages.First().Value;

        var query = new QueryExpression("contact");
        query.Criteria.AddCondition("accountid", ConditionOperator.Equal, context.PrimaryEntityId);

        var result = orgService.RetrieveMultiple(query);

        foreach (Entity contact in result.Entities)
        {
            contact["address1_line1"] = account.GetAttributeValue<string>("address1_line2");
            orgService.Update(contact);
        }
    }
}

在实体账户的 post 更新消息中注册并在步骤中添加 post 实体图像。

感谢 Henk Van Boeijen 上面的出色回答,我 post 以下代码(这是 Henk 的代码 post 编辑到我的插件中)。

这是一个插件的代码,当您更新组织的地址时,它将更新与组织相关联的所有联系人的所有地址。

请注意,在此示例中,组织实体已称为帐户。

这是为了帮助将来需要完成此任务的任何人。

public class UpdateContactAddresses : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        // Create a tracing instance to log progress of this plugin.
        ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        try
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Obtain the organization service reference.
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(null);

            if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity account = (pluginExecutionContext.InputParameters["Target"] as Entity);

                // Verify that the target entity represents an account. If not, this plug-in was not registered correctly.
                if (account.LogicalName != "account")
                {
                    tracing.Trace("This entity is not an Account entity. It is likely that this plug-in was not registered correctly (was an incorrect \"Primary Entity\" selected? It should be an Account entity).");
                    return;
                }

                var query = new QueryExpression("contact");
                query.Criteria.AddCondition("accountid", ConditionOperator.Equal, pluginExecutionContext.PrimaryEntityId);

                var result = service.RetrieveMultiple(query);
                tracing.Trace("The QueryExpression found " + result.TotalRecordCount.ToString() + " associated contacts.");

                foreach (Entity contact in result.Entities)
                {
                    tracing.Trace("Updating contact " + contact.ToString() + " address...");
                    contact["address1_line1"] = account.GetAttributeValue<string>("address1_line1");
                    contact["address1_line2"] = account.GetAttributeValue<string>("address1_line2");
                    contact["address1_line3"] = account.GetAttributeValue<string>("address1_line3");
                    contact["address1_city"] = account.GetAttributeValue<string>("address1_city");
                    contact["address1_county"] = account.GetAttributeValue<string>("address1_county");
                    contact["address1_postalcode"] = account.GetAttributeValue<string>("address1_postalcode");
                    contact["address1_country"] = account.GetAttributeValue<string>("address1_country");
                    service.Update(contact);
                    tracing.Trace("Contact " + contact.ToString() + " address updated.");
                }
            }

            tracing.Trace("Completed execution of plugin " + this.GetType().Name + ".");
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException("An error occurred in plugin " + this.GetType().Name + ".", ex);
        }
        catch (Exception ex)
        {
            tracing.Trace("An error occurred executing plugin " + this.GetType().Name + ".");
            tracing.Trace("\t\tError: " + ex.Message);
            throw ex;
        }

    }

}