Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 483
404 Avatar asked Dec 12 '25 13:12

404


1 Answers

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);
}
like image 68
Nicknow Avatar answered Dec 14 '25 04:12

Nicknow