我编写了一个函数来更新 CRM 2013 Online 上所有事件产品的默认价目表。
//The method takes IOrganization service and total number of records to be created as input
private void UpdateMultipleProducts(IOrganizationService service, int batchSize, EntityCollection UpdateProductsCollection, Guid PriceListGuid)
{
//To execute the request we have to add the Microsoft.Xrm.Sdk of the latest SDK as reference
ExecuteMultipleRequest req = new ExecuteMultipleRequest();
req.Requests = new OrganizationRequestCollection();
req.Settings = new ExecuteMultipleSettings();
req.Settings.ContinueOnError = true;
req.Settings.ReturnResponses = true;
try
{
foreach (var entity in UpdateProductsCollection.Entities)
{
UpdateRequest updateRequest = new UpdateRequest { Target = entity };
entity.Attributes["pricelevelid"] = new EntityReference("pricelevel", PriceListGuid);
req.Requests.Add(updateRequest);
}
var res = service.Execute(req) as ExecuteMultipleResponse; //Execute the collection of requests
}
//If the BatchSize exceeds 1000 fault will be thrown.In the catch block divide the records into batchable records and create
catch (FaultException<OrganizationServiceFault> fault)
{
if (fault.Detail.ErrorDetails.Contains("MaxBatchSize"))
{
var allowedBatchSize = Convert.ToInt32(fault.Detail.ErrorDetails["MaxBatchSize"]);
int remainingCreates = batchSize;
while (remainingCreates > 0)
{
var recordsToCreate = Math.Min(remainingCreates, allowedBatchSize);
UpdateMultipleProducts(service, recordsToCreate, UpdateProductsCollection, PriceListGuid);
remainingCreates -= recordsToCreate;
}
}
}
}
代码说明:系统中约有 5000 条有效产品记录。因此,我使用上面的代码更新所有这些的默认价目表。
但是,我在这里遗漏了一些东西,因此它只更新了 438 条记录。它正确地循环遍历 While 语句,但它没有更新此处的所有语句。
当我们第一次运行这个函数时,Batchsize 应该是多少?
有人可以帮我吗?
谢谢,
米塔尔。
最佳答案
您将 remainingCreates
作为 batchSize
参数传递,但您的代码从不引用 batchSize
,因此您只需重新输入该 while
每次循环。
此外,我不确定您是如何进行所有错误处理的,但您需要更新您的 catch
block ,以便它不会仅仅让错误异常通过(如果它们不这样做)包含一个 MaxBatchSize 值。现在,如果您针对批量大小以外的其他内容获取 FaultException
,它将被忽略。
{
if (fault.Detail.ErrorDetails.Contains("MaxBatchSize"))
{
var allowedBatchSize = Convert.ToInt32(fault.Detail.ErrorDetails["MaxBatchSize"]);
int remainingCreates = batchSize;
while (remainingCreates > 0)
{
var recordsToCreate = Math.Min(remainingCreates, allowedBatchSize);
UpdateMultipleProducts(service, recordsToCreate, UpdateProductsCollection, PriceListGuid);
remainingCreates -= recordsToCreate;
}
}
else throw;
}
关于c# - 动态CRM SDK : Execute Multiple Requests for Bulk Update of around 5000 Records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604713/