简体   繁体   中英

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.

My recommended approach is to create a new Entity() object for the update. This way your update code doesn't need to worry about what fields were retrieved, it just takes the ones it cares about updating.

foreach (var entity in entities.Entities)
{
    var newEntity = new Entity(entity.LogicalName, entity.Id);

    //Populate whatever fields you want (this is just an example)
    newEntity["new_somefield"] = entity.GetAttributeValue<string>("new_somefield").ToUpper();

    UpdateRequest updateRequest = new UpdateRequest { Target = newEntity };
    multipleRequest.Requests.Add(updateRequest);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM