如何使用 RelatedEntities 集合创建 RelatedEntities

How To Use The RelatedEntities Collection To Create RelatedEntities

我正在尝试遵循此处描述的逻辑:https://msdn.microsoft.com/en-us/library/gg309282.aspx 使用 RelatedEntities 属性 创建关联实体。问题是,没有创建关联实体。我正在尝试从 Pre=Operation 插件中执行此操作... PreOperation 插件不支持它吗?如果是我做错了什么?

代码如下:

var collection = new EntityCollection();
collection.Entities.AddRange(incentives.Select(e => e.ToSdkEntity()));
target.RelatedEntities.Add(new Relationship(new_LeadProduct.Fields.new_lead_new_leadproduct_LeadId), collection);

由于预创建插件在数据库中创建目标实体之前执行,您将无法创建引用目标的相关实体。您应该在 post-create 插件中执行相关的实体逻辑。

编辑: 如果您尝试在插件操作中创建与 Target 关联的相关记录,则此答案适用。你的问题没有特别说明,但根据你答案中的代码,这看起来不是你想要做的。

这是来自 MSDN 示例的代码:

//Define the account for which we will add letters                
Account accountToCreate = new Account
{
    Name = "Example Account"
};

//Define the IDs of the related letters we will create
_letterIds = new[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };

//This acts as a container for each letter we create. Note that we haven't
//define the relationship between the letter and account yet.
EntityCollection relatedLettersToCreate = new EntityCollection
{
    EntityName = Letter.EntityLogicalName,
    Entities =
    {
        new Letter{Subject = "Letter 1", ActivityId = _letterIds[0]},
        new Letter{Subject = "Letter 2", ActivityId = _letterIds[1]},
        new Letter{Subject = "Letter 3", ActivityId = _letterIds[2]}
    }
};

//Creates the reference between which relationship between Letter and
//Account we would like to use.
Relationship letterRelationship = new Relationship("Account_Letters");

//Adds the letters to the account under the specified relationship
accountToCreate.RelatedEntities.Add(letterRelationship, relatedLettersToCreate);

//Passes the Account (which contains the letters)
_accountId = _service.Create(accountToCreate);

经过一些额外的测试后,相关实体集合必须在 PreOperation 阶段之前填充。因此,在 PreValidation 上将其注册到 运行 会按预期工作。