gpt4 book ai didi

c# - ExecuteMultiple 与 Create 的不同行为

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:39 24 4
gpt4 key购买 nike

我有一个 Dynamics CRM 插件,可以在创建父机会时创建多个子记录。该插件被注册为一个post-operation,同步运行。

当我独立创建每个子记录时,一切正常:

Entity entity = (Entity)context.InputParameters["Target"];

do
{
var revenue = new Entity("new_opportunityrevenue");
revenue["lor_opportunityid"] = new EntityReference("opportunity", entity.Id);

// Create the child record - Works
service.Create(revenue);

currentYear++;
} while (currentYear <= lastYear);

但是,如果我切换到 ExecuteMultipleRequest,我会收到一个错误,指出每当它尝试创建记录时 Opportunity id 都不存在。这发生在第一个请求上,因此不会发生额外的处理。

Entity entity = (Entity)context.InputParameters["Target"];

var revenueRecords = new List<Entity>();

do
{
var revenue = new Entity("new_opportunityrevenue");
revenue["lor_opportunityid"] = new EntityReference("opportunity", entity.Id);

revenueRecords.Add(revenue);

currentYear++;
} while (currentYear <= lastYear);

// Create the request object
var request = new ExecuteMultipleRequest()
{
Settings = new ExecuteMultipleSettings()
{
ContinueOnError = false,
ReturnResponses = true
},
Requests = new OrganizationRequestCollection()
};

// Add a CreateRequest for each entity to the request collection
foreach(var entity in revenueRecords)
{
var createRequest = new CreateRequest { Target = entity };
request.Requests.Add(createRequest);
}

// Execute all the requests in the collection using a single method call - Fails
// Opportunity With Id = 4ea41651-538e-e711-8118-e0071b6ad141 Does Not Exist
var response = (ExecuteMultipleResponse)service.Execute(request);

为什么多次调用 Create() 有效,而使用 ExecuteMultipleRequest 调用 Execute() 失败?

编辑

我正在使用 Dynamics CRM Online (8.2.1.344)。下面是插件跟踪日志的 View ,显示了它用作机会 ID 的值,以及已创建的机会 ID。个别年份跟踪消息只是内存中的对象创建。

Matching Ids

最佳答案

基于 this post , ExecuteMultiple 在插件内运行时获取自己的数据库事务。如果这些事务没有嵌套,我想这就是您会收到该错误的原因,因为 opp 创建将在其自己的事务中尚未提交。

作为旁注,您希望在 ExecuteMultiple 中运行您的创建有什么好处吗?它的设计更多是为了减少多个请求的授权时间;在插件内部,使用它实际上不会获得任何性能。

关于c# - ExecuteMultiple 与 Create 的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com