我有一个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()
调用Execute()
失败时多次调用ExecuteMultipleRequest
可以工作?编辑
我正在使用Dynamics CRM Online(8.2.1.344)。这是插件跟踪日志的视图,显示了它用作商机ID的值以及已创建的商机ID。单个年份的跟踪消息只是在内存中创建对象。
最佳答案
基于this post,ExecuteMultiple在插件中运行时会获取自己的数据库事务。如果这些事务没有嵌套,那么我想这就是为什么会出现该错误的原因,因为opp的创建将出现在自己的尚未提交的事务中。
附带说明一下,在ExecuteMultiple中运行您的创建是否有任何好处?它旨在减少多个请求的身份验证时间;在插件中,使用它实际上并没有任何好处。
关于c# - ExecuteMultiple与Create的不同行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45984439/