Reputation: 277
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.
Upvotes: 2
Views: 6727
Reputation: 4255
There is no way you can update only fields without updating the Entity
. So even to update a single field you need to create an entity with Id
and set an attribute value and then create an UpdateRequest
setting the entity as Target
and then after update the modifiedon
date changes to the time of update.
There is already codes in other answers telling how to create that but, you can also do Entity.Attributes.Add("name", "vinod");
to add a new field value. For your case since it is an update you probably have the entity object with you, so you can simply try to remove other fields as Entity.Attributes.Remove("name");
if you have not put a filter while fetching and you have all the fields.
To update multiple records you simply need to have Entity.LogicalName
,Entity.Id
and the update field values and send it to the UpdateRequest
. and please note without an Entity.Id
it will try to create it.
But 200,000 records is too much and I won't suggest using API rather do Excel data import as it is designed for data import.
Upvotes: 0
Reputation: 22836
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.
Upvotes: 3
Reputation: 7224
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);
}
Upvotes: 8