Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Batching an entitycollection for ExecuteMultipleRequest

Posted on by 1,575

Hello,.

Has anyone any advice on how best to split up a large entitycollection (10,000+ records) so that I can use ExecuteMultipleRequest and not breach the Maxbatch 1000 limit.

So for 10000 records I want to split the master entitycollection into 10 smaller entitycollections?

I saw the MoreLinq BATCH() function and it looked perfect but if I try

foreach (var mybatch in p.newcontactcollection.Entities.Batch(1000))

I do not know how to cast mybatch to an EntityCollection.

Does anyone know if this is possible either using MoreLinq.Batch (preferably) or any other fairly simple implementation?

Many thanks as ever,


- Seamus

*This post is locked for comments

  • Seamus Profile Picture
    Seamus 1,575 on at
    RE: Batching an entitycollection for ExecuteMultipleRequest

    Guido you have been an incredible help.

    Thanks so much for sharing your time and knowledge!

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Batching an entitycollection for ExecuteMultipleRequest

    Hi Seamus,

    I don't know which examples you checked (also there are many ways to do things inside CRM, so what I write is the way I normally do)

    assuming you want to do an ExecuteMultipleRequest, the Requests parameter is an OrganizationRequestCollection, so you need to create a  collection of CreateRequest, UpdateRequest, etc etc.

    Personally I don't like to mix different kind of requests (although is possible) so I do an example with a CreateRequest.

    starting from using the ChunkBy method I wrote before, this is a simplified code (I use some helpers but this is what it does in the end)

    List<List<Entity>> splittedLists = entities.ChunkBy<Entity>(1000);
    foreach (List<Entity> batch in splittedLists)
    {
        // create the list of CreateRequest
        List<CreateRequest> createRequests = new List<CreateRequest>();
        foreach (Entity entity in batch)
        {
            CreateRequest createRequest = new CreateRequest();
            createRequest.Target = entity;
            createRequests.Add(createRequest);
        }

        // create the request collection
        OrganizationRequestCollection requestCollection = new OrganizationRequestCollection();
        requestCollection.AddRange(createRequests);

        // create the execute multiple and set the requestCollection
        ExecuteMultipleRequest execRequest = new ExecuteMultipleRequest()
        {
            Settings = new ExecuteMultipleSettings()
            {
                ContinueOnError = true,
                ReturnResponses = true
            },
            Requests = requestCollection
        };
        ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)service.Execute(execRequest);
    }

  • Seamus Profile Picture
    Seamus 1,575 on at
    RE: Batching an entitycollection for ExecuteMultipleRequest

    Many thanks Guido, that works and I can achieve my list of batches of 900, but everywhere I see ExecuteMultipleRequest requestWithResults being implemented it is used with an entitycollection, so am I better to re-convert my (list) batches to individual entitycollections of 900 and then send theses entiycollections to add to the ExecuteMultipleRequest as CreateRequests?  Thanks again.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Batching an entitycollection for ExecuteMultipleRequest

    personally I use this List extension (I first convert my Entities to a List (with a ToList()) and after use this extension like

     List<List<Entity>> splittedLists = myEntities.ChunkBy<Entity>(500);

    and deal with two foreach

    the code is

    public static class ListExtensions
    {
      public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
      {
        return source
        .Select((x, i) => new { Index = i, Value = x })
        .GroupBy(x => x.Index / chunkSize)
        .Select(x => x.Select(v => v.Value).ToList())
        .ToList();
      }
    }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans