使用Dynamics crm中的插件将多个记录关联到子网格

时间:2014-06-17 11:13:15

标签: dynamics-crm-2011 dynamics-crm microsoft-dynamics dynamics-crm-2013

我正在使用AssociateRequest将记录与其他实体相关联。我的问题是如何将多个记录关联到subgrid。你可以告诉任何人澄清我。

Entity en=(Entity)context.InputParameters["Target"];
                AssociateRequest assreq = new AssociateRequest();
                assreq.Target = new EntityReference(en.LogicalName,en.Id);
                assreq.RelatedEntities = new EntityReferenceCollection();
                assreq.RelatedEntities.Add(new EntityReference("contact", new Guid("72C8B80B-FEF1-E311-9345-D89D67642EB0")));
                assreq.Relationship = new Relationship("contact_customer_accounts");
                AssociateResponse assresponse = (AssociateResponse)service.Execute(assreq);

2 个答案:

答案 0 :(得分:0)

AssociateRequest只能用于创建两个记录之间的关系。如果您需要多次关联记录,则必须为每个关系重复此过程。

您可以将AssociateRequest包装到ExecuteMultipleRequest中,并将一些往返保存到服务器。请记住:ExecuteMultipleRequest中的请求不参与同一个数据库事务。

答案 1 :(得分:0)

OrganizationRequestCollection associateRequestCollection = new OrganizationRequestCollection();
            Entity en = (Entity)context.InputParameters["Target"];
  

您必须为每一个创建AssociateRequest   关系并将它们添加到associateRequestCollection对象中,如下所示

AssociateRequest assreq = new AssociateRequest();
        assreq.Target = new EntityReference(en.LogicalName, en.Id);
        assreq.RelatedEntities = new EntityReferenceCollection();
        assreq.RelatedEntities.Add(new EntityReference("contact", new Guid("72C8B80B-FEF1-E311-9345-D89D67642EB0")));
        assreq.Relationship = new Relationship("contact_customer_accounts");
        associateRequestCollection.Add(assreq);
  

//处理针对联系人ID创建多封电子邮件;

ExecuteMultipleResponse emResponses = ExecuteCRMMultipleRequest(associateRequestCollection);
private ExecuteMultipleResponse ExecuteCrmMultipleRequest(OrganizationRequestCollection associateRequestCollection)
        {
            ExecuteMultipleRequest emRequest = new ExecuteMultipleRequest
            {
                Requests = associateRequestCollection,
                Settings = new ExecuteMultipleSettings
                {
                    ContinueOnError = true,
                    ReturnResponses = true
                }
            };

            return (ExecuteMultipleResponse)service.Execute(emRequest);
        }