简体   繁体   English

使用ExecuteTransactionRequest创建相关实体

[英]Using ExecuteTransactionRequest to create related entities

I've got Entity types that are in a parent-child relationship. 我有处于父子关系的Entity类型。

Since ExecuteTransactionRequest executes multiple message requests in one tranasaction, would the following work as I intend it to? 由于ExecuteTransactionRequest事务中执行多个消息请求,因此以下功能是否可以按我的预期工作?

There are 3 parents with no children to start with: 有3个没有孩子的父母开始了:

//Create a 4th parent
cs_parent parent4 = new cs_parent{ cs_name = "p4" };
CreateRequest createParentRequest = new CreateRequest { Target = parent4 };
request.Requests.Add(createParentRequest);

EntityCollection parents 
  = context.RetrieveMultiple(/*fetchExpression to get all parents (I'm expecting 4 now)*/);

//Create a child for each parent
foreach (var p in parents.Entities)
{
  cs_child child = new cs_child
  {
    cs_parentid = p.ToEntityReference();
  }
  CreateRequest createChildRequest = new CreateRequest { Target = child };
  request.Requests.Add(createChildRequest);
}
response = (ExecuteTransactionResponse)context.Execute(request);

Would I be getting 4 parents with one child each then, or only 3 since when I'm retrieving multiple, the 4th one hasn't been created yet (?)? 那我是要让4个父母每个孩子带一个孩子,还是只有3个父母,因为当我检索多个孩子时,还没有创建第4个父母(?)?

If not, how do I revise my code ideally with still one Execute command at the end? 如果没有,我如何在理想情况下最后还是Execute 一个 Execute命令来修改我的代码?

I haven't actually run your code for myself to be 100% sure, but it looks like it's going to error out because the fourth parent record doesn't have the necessary info on it at the time you assign it as an EntityReference on the child entity. 我实际上并没有为您自己确保100%的代码运行,但是看起来它会出错,因为在将其分配为EntityReference ,第四条父记录没有必要的信息。子实体。 You can work around this easily though. 您可以轻松解决此问题。 CRM allows for this type of situation where inter-dependent records can all be submitted within one batch Create request. CRM允许这种情况,其中相互依赖的记录都可以在一个批Create请求中提交。 Normally when you create a record in CRM, the system assigns it a unique identifier (guid), but you can override this simply by assigning the guid yourself, then you have what you need to set it as a EntityReference on other objects. 通常,当您在CRM中创建记录时,系统会为其分配一个唯一的标识符(guid),但是您可以简单地通过自己分配该guid来覆盖它,然后您需要将其设置为其他对象上的EntityReference So when you create the fourth parent, you would have something like this: 因此,当您创建第四个父级时,您将具有以下内容:

cs_parent parent4 = new cs_parent { cs_name = "p4",cs_parentId = Guid.NewGuid());

Just guessing at the Id field on your entity, but you get the idea. 只是猜测您实体上的ID字段,但您就知道了。

One thing I'm not sure from your code sample, what context is, so I can't say for sure if doing a retrieve on that will return your parent4 object. 从代码示例中我不确定一件事,即context是什么,因此我无法确定是否对此进行检索是否会返回您的parent4对象。 You might need to have two loops, one for existing cs_parent records to create child records for them, and another loop to create child records for parent records in the request.Requests list that are not yet in the system... Food for thought. 您可能需要有两个循环,一个循环用于现有的cs_parent记录cs_parent其创建子记录,另一个循环以创建request.Requests列表中系统中尚未存在的父记录的子记录。

Edit: I realise I misread part of the question, but the following still applies to the new parent record you want to create. 编辑:我知道我误解了一部分问题,但是以下内容仍然适用于您要创建的父记录。 Add it to the ExecuteTransactionRequest Requests. 将其添加到ExecuteTransactionRequest请求中。

Add the children to the parent Entity's RelatedEntities collection (pseudo-example): 将子级添加到父实体的RelatedEntities集合中(伪示例):

// Create parent object
var invoice = new Entity("invoice");
// Create list of child objects
var invoiceDetailList = new List<Entity>() { new Entity("invoicedetail"), new Entity("invoicedetail") };
// Add child records to parent record's RelatedEntities
invoice.RelatedEntities.Add(new Relationship("invoice_invoicedetails"), new EntityCollection(invoiceDetailList));
// Add to ExecuteTransactionRequest.
transactionRequest.Requests.Add(new CreateRequest { Target = invoice });

This way you don't need to know the parent record's GUID up front. 这样,您无需预先知道父记录的GUID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM