Dynamics CRM SDK:执行约5000条记录的批量更新的多个请求
本文关键字:更新 请求 记录 SDK CRM 执行 5000条 Dynamics | 更新日期: 2023-09-27 17:53:00
我编写了一个函数来更新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语句,但这里并没有更新所有的While语句。
第一次运行这个函数时,Batchsize应该是多少?
这里有人能帮我吗?谢谢你,
米塔尔。
您传递remainingCreates
作为batchSize
参数,但您的代码从未引用batchSize
,因此您每次都将重新进入while
循环。
另外,我不确定你是如何做你所有的错误处理,但你需要更新你的catch
块,使它不只是让FaultExceptions通过,如果他们不包含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;
}
而不是被动处理,我更喜欢主动处理MaxBatchSize,这是真的,当你已经知道什么是MaxMatchSize。
以下是示例代码,在这里添加OrgRequest到集合时,我保留批处理的计数,当它超过时,我调用Execute并重置集合以获取新的批处理。
foreach (DataRow dr in statusTable.Rows)
{
Entity updEntity = new Entity("ABZ_NBA");
updEntity["ABZ_NBAid"] = query.ToList().Where(a => a.NotificationNumber == dr["QNMUM"].ToString()).FirstOrDefault().TroubleTicketId;
//updEntity["ABZ_makerfccall"] = false;
updEntity["ABZ_rfccall"] = null;
updEntity[cNBAttribute.Key] = dr["test"];
req.Requests.Add(new UpdateRequest() { Target = updEntity });
if (req.Requests.Count == 1000)
{
responseWithResults = (ExecuteMultipleResponse)_orgSvc.Execute(req);
req.Requests = new OrganizationRequestCollection();
}
}
if (req.Requests.Count > 0)
{
responseWithResults = (ExecuteMultipleResponse)_orgSvc.Execute(req);
}