我编写了一个函数来更新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应该是什么?
任何人都可以帮助我吗?
谢谢,
塔尔。
答案 0 :(得分:2)
您将remainingCreates
作为batchSize
参数传递,但您的代码从不引用batchSize
,因此您每次都要重新输入while
循环。
另外,我不确定你是如何进行所有错误处理的,但是你需要更新你的catch
块,这样如果它们不包含{{{}}那么它就不会让它通过{1}}价值。现在,如果您对批量大小以外的其他内容采用MaxBatchSize
,则会被忽略。
FaultException
答案 1 :(得分:1)
而不是反应式处理,我更喜欢主动处理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);
}