Search code examples
c#dynamics-crmdynamics-crm-onlinebulkupdate

Dynamics CRM SDK : Batch update specific fields in entity


I am new to Dynamics CRM development. I want to batch update certain fields in Entity using Batch update method in Dynamics CRM Online. I am using below code for performing batch update:

var multipleRequest = new ExecuteMultipleRequest()
{
    Settings = new ExecuteMultipleSettings()
    {
        ContinueOnError = false,
        ReturnResponses = true
    },
    Requests = new OrganizationRequestCollection()
};

foreach (var entity in entities.Entities)
{
    UpdateRequest updateRequest = new UpdateRequest { Target = entity };
    multipleRequest.Requests.Add(updateRequest);
}

ExecuteMultipleResponse multipleResponse = (ExecuteMultipleResponse)service.Execute(multipleRequest);

How can I specify only fields which I want to update instead of entire entity being updated?

Note: I have around 200,000 records to update using the above code. Currently it takes around 1.5 minute to update a single batch of 1000 records. So was thinking a way to update only required fields.


Solution

  • You have to look at the way how the EntityCollection entities is filled up. If retrieving using RetrieveMultiple, then Pull the minimal fields may be the native Name field & PK Id field will come by default. This way not the whole entity will be updated back.

    Avoid using AllColumns = true. Use ColumnSet to get minimal fields needed for validation.

    ColumnSet = new ColumnSet("field_needed"),
    

    Next, assign only the necessary fields like below inside loop.

    foreach (var entity in entities.Entities)
        {
            UpdateRequest updateRequest = new UpdateRequest { Target = entity };
    
            entity.Attributes["field_to_update"] = "field_value";
    
            multipleRequest.Requests.Add(updateRequest);
        }
    

    My answer will help you to understand what went wrong & correcting it. Like Nicknow said, you can assign fresh entity to solve issue.