Question

I have written a function to update Default Price List for all the Active Products on the 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;
            }
        }
    }
}

Code Description : There are around 5000 active product records in the System. So I am updating Default Price List for all of them using above code.

But, I am missing here something so that, it has updated only 438 records. It loops through the While statement correctly, but it is not updating all of them here.

What should be the Batchsize when we run this function for the First Time?

Any one can help me here?

Thank you,

Mittal.

Was it helpful?

Solution

You pass remainingCreates as the batchSize parameter but your code never references batchSize so you are just going to reenter that while loop every time.

Also, I'm not sure how you are doing all your error handling but you need to update your catch block so that it doesn't just let FaultExceptions pass-through if they don't contain a MaxBatchSize value. Right now, if you take a FaultException regarding something other than batch size it will be ignored.

{
    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;
}

OTHER TIPS

Instead of reactive handling, i prefer proactive handling of the MaxBatchSize, this is true when you already know what is MaxMatchSize is.

Following is sample code, here while adding OrgRequest to collection i keep count of batch and when it exceeds I call Execute and reset the collection to take fresh batch.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top