gpt4 book ai didi

c# - 动态 CRM SDK : Execute Multiple Requests for Bulk Update of around 5000 Records

转载 作者:行者123 更新时间:2023-11-30 22:07:58 25 4
gpt4 key购买 nike

我编写了一个函数来更新 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 ,这样它才不会让 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;
}

关于c# - 动态 CRM SDK : Execute Multiple Requests for Bulk Update of around 5000 Records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604713/

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