locked
Execute Multiple Requests for Bulk Update of around 5000 Records RRS feed

  • Question

  • Hello,

    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.


    Monday, March 24, 2014 7:44 AM

All replies

  • Since it's only ~5000 updates you could just remove the ExecuteMultiple and update them individually. Unless you're running the app on a different server to CRM it will be about the same speed anyway.

            private void UpdateMultipleProducts(IOrganizationService service, int batchSize, EntityCollection UpdateProductsCollection, Guid PriceListGuid)
            {
                UpdateProductsCollection.Entities.ToList().ForEach(a =>
                {
                    Entity update = new Entity("product");
                    update["productid"] = a.Id;
                    update["pricelevelid"] = new EntityReference("pricelevel", PriceListGuid);
    
                    service.Update(update);
                });
            }

    Hope that helps

    Paul


    If my response helped you find your answer please show your thanks by taking the time to "Mark As Answer" and "Vote As Helpful".

    Twitter LinkedIn Facebook Blog Magnetism

    Wednesday, March 26, 2014 7:02 AM
  • Update multiple doesn't allow more than 1000 records at a time. Try below code. I have tried something similar to this and it is working successfully for 4000 records.

     int count = 1;
            foreach (var entity in UpdateProductsCollection.Entities)
            {
                entity.Attributes["pricelevelid"] = new EntityReference("pricelevel", PriceListGuid);
                UpdateRequest updateRequest = new UpdateRequest { Target = entity };
                req.Requests.Add(updateRequest);
                if (req.Count == 1000)
                {
                    var res = service.Execute(req) as ExecuteMultipleResponse;  //Execute the collection of requests
                    req.Clear();
                }
            }

            if (req.Count > 0)
            {
                var res = service.Execute(req) as ExecuteMultipleResponse;  //Execute the collection of requests
            }

    Thursday, March 27, 2014 2:01 AM