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

时间:2018-05-30 08:18:00

标签: c# plugins dynamics-crm crm

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

我有"account" entity已关联"contacts".当更新当前"account" entity's "address"时,我希望所有关联的联系人地址都使用该地址进行更新。

我希望在更新地址时在"account"实体上运行的插件中执行此操作。执行此操作时,所有关联的联系人都将其地址更新为该地址。

我已经做了一些搜索,但没有任何显示ADDRESS得到更新的内容。那里的示例通常示出例如正在更新的电话号码。地址更复杂的原因是地址存储在一个地址实体中,所以我想我必须得到某种addressId primary key并将其放在每个associated contacts address FK field.我的地址中不知道如何找到任何类似的例子。

  

有没有人会有一段代码片段进入插件?

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

2 个答案:

答案 0 :(得分:0)

更新时,您不需要联系人地址的ID。两种类型的地址已经包含在联系实体中。它们通常用于邮寄和访问地址。地址字段名称以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);
        }
    }
}

在实体帐户的帖子更新消息上注册它,并将一个帖子实体图像添加到该步骤。

答案 1 :(得分:0)

感谢上面的Henk Van Boeijen的优秀答案,我发布了以下代码(这是Henk的代码发布到我的插件中)。

以下是插件的代码,当您更新组织的地址时,该插件将更新连接到组织的所有联系人的所有地址。

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

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

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;
        }

    }

}