通过查找实体引用

时间:2018-11-07 10:21:26

标签: c# plugins dynamics-crm

/ *我创建了一个自定义实体名称“帐户副本”。当我在“帐户”实体中创建记录时,该记录的副本也会在“帐户副本”中创建。到目前为止,到目前为止还不错,但是当我尝试更新帐户实体记录时,帐户副本实体记录也应该更新。为此,我需要通过无法查明的查找字段引用实体。请指导我。* /

using System;
using Microsoft.Xrm.Sdk;

namespace CreateUpdate
{
    public class CreateUpdate : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.InputParameters != null)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "account") return;

                else if (entity.LogicalName == "account")
                {
                    try
                    {
                        if (context.MessageName == "Create")
                        {
                            //call create method
                            CreateRecord(service, entity);
                        }

                        else if (context.MessageName == "Update")
                        {
                            //call Update method
                            UpdateRecord(context, service, entity);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException(ex.Message);
                    }
                }

            }
        }
        private static void CreateRecord(IOrganizationService service, Entity account)
        {
            var accountcopy = new Entity("new_accountcopy");
            accountcopy["new_name"] = account.GetAttributeValue<string>("name");
            accountcopy["new_phone"] = account.GetAttributeValue<string>("telephone1");
            accountcopy["new_fax"] = account.GetAttributeValue<string>("fax");
            accountcopy["new_website"] = account.GetAttributeValue<string>("websiteurl");
            service.Create(accountcopy);

        EntityReference Entref = (EntityReference)account.Attributes["accountid"];
        Guid LookupId = Entref.Id;
        string logicalName = Entref.LogicalName;
        accountcopy["new_accountcopyid"]= new EntityReference(logicalName, LookupId);
        accountcopy["new_accountcopyid"] = null;
        }

        private static void UpdateRecord(IPluginExecutionContext context, IOrganizationService service, Entity account)
        {
            account.Attributes["name"] = "Max";
            account.Attributes["telephone1"] = "+422565895";
            account.Attributes["fax"] = "+426565898";
            account.Attributes["websiteurl"] = "www.max.com";

            if (context.Depth <= 1)
            {
                service.Update(account);
            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

关于如何处理它的想法(请注意,我没有测试此代码):

<div>loading...</div>

而且,您可能希望添加处理以删除帐户,以同时删除AccountCopy。

答案 1 :(得分:0)

如果此插件在account实体的创建消息的后操作中注册,假设new_accountcopy实体具有accountid查找关系,则下面的代码将起作用。

Entity accountCopy = new Entity("new_accountcopy");

accountCopy["new_name"] = entity.Attributes["name"];
accountCopy["new_phone"] = entity.Attributes["telephone1"];
accountCopy["new_fax"] = entity.Attributes["fax"];
accountCopy["new_website"] = entity.Attributes["websiteurl"];
accountCopy["accountid"] = new EntityReference("account", entity.Id); //mapping the account lookup in account-copy record

service.Create(accountCopy);

额外提示:string LookUpaccountName = entref.LogicalName;是正确的方式

更新:我们正在谈论两种不同的事情-映射关系查找,然后使用该查找同步来自进一步更新的数据。

场景1 (这是您的原始问题)

在创建新帐户记录时-在帐户实体上发布创建插件会创建帐户复制记录,并且其中一个记录将在查找中引用其他记录。我认为这种关系永远不会改变-无需同步帐户记录更新。

如果您在accountid实体中进行了new_accountcopy查找-我的上述代码可以正常工作。
(或者)
如果您在new_accountcopyid实体中有account个查询,请遵循以下代码:

Guid createdId = service.Create(accountcopy);

Entity toUpdate = new Entity("account");
toUpdate.Id = entity.Id;
toUpdate["new_accountcopyid"]= new EntityReference("new_accountcopy", createdId); //mapping the account-copy lookup in account record
service.Update(toUpdate);

场景2

在将来更新主帐户时,您可以检索相关的帐户副本记录并通过帐户更改对其进行更新。

注意:让插件处于异步模式。