简体   繁体   中英

Update only some fields in a Microsoft Dynamics entity

I'm developing a software in C# language that interacts with Microsoft Dynamics through calls to Organization service.

I need to update only some fields of the entity, but when I call the Update operation, Microsoft Dynamics launches a massive update of all the fields of the entity. I don't want this massive update, I need to update only 2 or 3 fields.

This is my code:

//reading account
Entity account = <reading entity with a call to organizationService>
//update fields
account["fieldName1"] = newValue;
account["fieldName2"] = newValue2;
//call to update entity
organizationService.Update(account);

How I can solve this problem?

We use early binding here and we also accept the entire object will be updated, but from memory: try this

var accountToBeSentToDynamics = new Entity("account");
accountToBeSentToDynamics.Id = accountToBeUpdated.Id;
accountToBeSentToDynamics["name"] = "Only This Field will be updated";

We also add these to a list of inserts or updates and flush them in batches of size 1000 or less to speed performance

This is your problem.

Entity account = <reading entity with a call to organizationService>;

Any attributes retrieved within the call to the org service will be present on your "account" object and subsequently resent to CRM in the update message.

Try something like this:

Entity retrieved = <reading entity with a call to organizationService>;
Entity account = new Entity("account", retrieved.Id);
//update fields
account["fieldName1"] = newValue;
account["fieldName2"] = newValue2;
//call to update entity
organizationService.Update(account);

This way you are guaranteed that only the attributes you've added to the object are updated in CRM.

Lastly, its best practice to only retrieve attributes necessary to perform your business logic when doing a Retrieve or RetrieveMultiple. More attributes will reduce the performance of the query.

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