D365 CRM: Execute server-side bulk operations in Dynamics CRM



  • In Microsoft Dynamics 365 Customer Engagement server-side automation, ex. plugins and custom workflow activities, we can write code to create, retrieve, update and delete individual records. When we need to carry out these operations on multiple records, we can use a loop and query the database for each record. However, a much more efficient way to carry out bulk operations is to use the ExecuteMultipleRequest message (to Create, Update and Delete records in bulk). In this example I will show a code to execute a bulk update of thousands of accounts records that would make the process a bit slow if you do it by one by one.

    1.    EntityCollection accountActiveCollection = GetActiveAccounts(); // Your query to get the accounts
    2.    List<Entity> accountList = new List<Entity>();
    3.     
    4.    foreach (Entity account in accountActiveCollection.Entities)
    5.    {
    6.        Entity up = new Entity("account", account.Id);
    7.        up["remcod_legalname"] = account["name"].ToString();
    8.     
    9.        accountList.Add(up);
    10.     
    11.        if (accountList.Count > 500) // When the batch list is more than 500 then I will call the execute request to update                
    12.        {
    13.            Console.WriteLine("Update");
    14.     
    15.            ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest();
    16.            requestWithResults = new ExecuteMultipleRequest()
    17.            {
    18.                // Assign settings that define execution behavior: continue on error, return responses. 
    19.                Settings = new ExecuteMultipleSettings()
    20.                {
    21.                    ContinueOnError = true,
    22.                    ReturnResponses = false
    23.                },
    24.                // Create an empty organization request collection.
    25.                Requests = new OrganizationRequestCollection()
    26.            };
    27.     
    28.            foreach (Entity entity2 in accountList)
    29.            {
    30.                UpdateRequest UpdateRequest = new UpdateRequest { Target = entity2 };
    31.                requestWithResults.Requests.Add(UpdateRequest);
    32.            }
    33.     
    34.            try
    35.            {
    36.                ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)Service.Execute(requestWithResults);
    37.            }
    38.            catch (Exception ex)
    39.            {
    40.                Console.WriteLine(ex.ToString());
    41.            }
    42.     
    43.            accountList.Clear(); // Clean the list to start again to fill it until 500
    44.        }
    45.    }
    46.     
    47.    if (accountList.Count > 0) // Execute the remaining records
    48.    {
    49.        Console.WriteLine("Update");
    50.     
    51.        ExecuteMultipleRequest requestWithResults = new ExecuteMultipleRequest();
    52.        requestWithResults = new ExecuteMultipleRequest()
    53.        {
    54.            // Assign settings that define execution behavior: continue on error, return responses. 
    55.            Settings = new ExecuteMultipleSettings()
    56.            {
    57.                ContinueOnError = true,
    58.                ReturnResponses = false
    59.            },
    60.            // Create an empty organization request collection.
    61.            Requests = new OrganizationRequestCollection()
    62.        };
    63.     
    64.        foreach (Entity entity2 in accountList)
    65.        {
    66.            UpdateRequest UpdateRequest = new UpdateRequest { Target = entity2 };
    67.            requestWithResults.Requests.Add(UpdateRequest);
    68.        }
    69.     
    70.        try
    71.        {
    72.            ExecuteMultipleResponse responseWithResults = (ExecuteMultipleResponse)Service.Execute(requestWithResults);
    73.        }
    74.        catch (Exception ex)
    75.        {
    76.            Console.WriteLine(ex.ToString());
    77.        }
    78.     
    79.        accountList.Clear();
    80.    }

    Let explain the code:
    -First, I get by query all the active accounts.
    -Then for each one I take the account name and set the remcod_legalname (custom field) with that value (here put your business operation).
    -I add in a custom List collection, the record to update.
    -When that list has more than 500 items, then I will generate ExecuteMutipleRequest with these records to update and clean the list. I recommend this max number of items to send to the request to not generate time out exceptions in CRM.
    -When the for each end, I check if the list contains some remaining account record to execute them.

    Original Source:
    https://itsfascinating.com/d365/tag/updaterequest/

    Official Documentation:
    https://docs.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.messages.updaterequest?view=dataverse-sdk-latest
    Enjoy it!


  • Comments



Add a Comment