4
\$\begingroup\$

I have a method which takes a list of Entity objects, loop through elements in that list and add them to a "ExecuteMultipleRequest" object. After that the ExecuteMultipleRequest will be executed.

However, according to Microsoft Dynamic CRM specs, I can only add at most 1000 entities to ExecuteMultipleRequest at one time, or else that request will throw an error when executed, and my list might have more than 1000 elements.

I have come up with the following solution to make sure that at least 1 request will be executed for every 1000 elements, but I find it to be inelegant. What can I improve here?

private readonly int MAXTOBEPROCESSED = 1000;

public void processEntities (List<Entity> entities)
{
    var toBeProcessed = 0;
    var createMultiple = new ExecuteMultipleRequest()
    {
        Settings = new ExecuteMultipleSettings()
        {
            ContinueOnError = false,
            ReturnResponses = true
        },
        Requests = new OrganizationRequestCollection()
    };

    foreach (var entity in entities)
    {
        var createRequest = new CreateRequest{ Target = entity };
        createMultiple.Requests.Add(createRequest);

        if (++toBeProcessed >= MAXTOBEPROCESSED)
        {
            var responseWithResults = (ExecuteMultipleResponse)service.Execute(createMultiple);

            createMultiple = new ExecuteMultipleRequest()
            {
                Settings = new ExecuteMultipleSettings()
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                },
                Requests = new OrganizationRequestCollection()
            };
            toBeProcessed = 0;
        }
    }

    if (toBeProcessed != 0)
    {
        var responseWithResults = (ExecuteMultipleResponse)service.Execute(createMultiple);
    }
}
\$\endgroup\$
  • \$\begingroup\$ I think this looks quite good but if you are looking for a compact linq solution then Split List into Sublists with LINQ might be exactly what you are looking for. \$\endgroup\$ – t3chb0t Jul 29 '16 at 6:16
  • \$\begingroup\$ what is inelegant in your opinion ? \$\endgroup\$ – Disappointed Jul 29 '16 at 7:06
  • \$\begingroup\$ @Disappointed I'm mainly concerned that the same ExecuteMultipleRequest has to be initialized in 2 places. And I'm not sure that adding an if to the end is the best way to make sure that all remaining elements are processed. \$\endgroup\$ – tsukumogami Jul 29 '16 at 7:27
1
\$\begingroup\$

Your logic looks fine to me. If you find yourself creating something in two places then feel free to create a helper function to do so.

Your logic can be arguably simpler if you count down rather than up, since we already know the number of requests.

Something like:

private static int MAXTOBEPROCESSED = 1000;

public void processEntities (List<Entity> entities)
{
    var createMultiple = CreateMultipleRequest();

    for (int n = entities.Count; n > 0; n--)
    {
        createMultiple.Requests.Add(new CreateRequest{ Target = entities[n] });

        if (n % MAXTOBEPROCESSED == 0)
        {
            var responseWithResults = (ExecuteMultipleResponse)service.Execute(createMultiple);
            createMultiple = CreateMultipleRequest();
        }
    }
}

private CreateMultipleRequest()
{
    return new ExecuteMultipleRequest()
    {
        Requests = new OrganizationRequestCollection(),
        Settings = new ExecuteMultipleSettings()
        {
            ContinueOnError = false,
            ReturnResponses = true
        }
    };
}
\$\endgroup\$
  • \$\begingroup\$ Creating a helper seems like a good idea to me, but there is a problem with your code. I might have more than 1000 entities, but the number of entities is not necessary a multiples of 1000. If I have 2100 entities for example, your code will leave the last 100 entities unprocessed. \$\endgroup\$ – tsukumogami Aug 1 '16 at 0:44
  • \$\begingroup\$ It shouldn't do. It will do the 100 first, then two lots of 1000. \$\endgroup\$ – GazTheDestroyer Aug 1 '16 at 8:08
  • \$\begingroup\$ You are right, my bad. \$\endgroup\$ – tsukumogami Aug 1 '16 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.