web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics CRM SDK: Execute Multiple Requests for Bulk Update of around 5000 Records

(0) ShareShare
ReportReport
Posted on by

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 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.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    rath.amit38@gmail.com Profile Picture
    2 on at

    Hi,

    Maximum batch size – there is a limit to how many requests can be added to a request collection. If that limit is exceeded, a fault is thrown before the first request is ever executed. A limit of 1000 requests is typical though this maximum amount can be set for the Microsoft Dynamics 365 deployment. The deployment setting for this limit is BatchSize.

    Please follow the below link for more details.

    https://msdn.microsoft.com/en-us/library/jj863631.aspx

    Thanks,

    Amit Kumar Rath

  • Suggested answer
    rath.amit38@gmail.com Profile Picture
    2 on at

    What should you do when your input request collection exceeds the maximum batch size?

                                                                                                                                         Answer :    

    Your code can’t directly query the maximum batch size through the deployment web service unless it is run under an account that has the deployment administrator role.

  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     when the errors happens (batch size), it does not actually update anything.

     So, in your scenario.. it skips, let's say, the first 5000.. And, then, it runs successfully for the remaining 438

     You should change the logic to only process up to allowedBatchSize records per each executemultiple.

    foreach (var entity in UpdateProductsCollection.Entities)

     {

       UpdateRequest updateRequest = new UpdateRequest { Target = entity };

       entity.Attributes["pricelevelid"] = new EntityReference("pricelevel", PriceListGuid);

       req.Requests.Add(updateRequest);

       if(req.Requests.Count == allowedBatchSize)

      {

         service.Execute(req);

         req.Requests.Clear();

      }

    }

    //In case there are leftovers

    service.Execute(req);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans