Answered by:
CRM 4 Web Service: How to update only one field

Question
-
Hello fellow developpers,
I query a CRM entity with all fields every time I plan to update that entity. Then I do:
this.crmService.Update(prospect);
But querying all fields is slow. What would be a better way of doing this?
For instance, if I want to update Field1 for all the leads with the formula Field2 - Field3, is there a way to update the lead if I query only those three fields and leadid?
Thank you very much!
Mathieu
Thursday, February 17, 2011 5:06 PM
Answers
-
You should use the ColumnSet class to retrieve only set of specified attributes.
http://msdn.microsoft.com/en-us/library/bb889187.aspx
http://msdn.microsoft.com/en-us/library/bb929002.aspx
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Proposed as answer by Jim Glass Jr Thursday, February 17, 2011 9:13 PM
- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Thursday, February 17, 2011 8:50 PMModerator -
Something along these lines:
TargetRetrieveLead target = new TargetRetrieveLead(); target.EntityId = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); RetrieveRequest reqTarget = new RetrieveRequest(); reqTarget.ColumnSet = new ColumnSet(new string[] { "field2", "field3" }); reqTarget.ReturnDynamicEntities = false; reqTarget.Target = target; RetrieveResponse respTarget = (RetrieveResponse)crmService.Execute(reqTarget); lead lead = (lead)respTarget.BusinessEntity; lead.field1 = lead.field2 = lead.field3; crmService.Update(lead);
When updating an existing entity instance, you only need to populate properties for those attributes that you wish to modify. The Primary Key GUID is always populated, even if you don't specify it, in your ColumnSet.
--pogo (pat)- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 12:37 AM -
it depends on if your using dynamic entities or if you have business entitites from a web reference in your project.
Here is examples using a dynamic entity, you'll see an Update example in the code http://msdn.microsoft.com/en-us/library/aa685870.aspx
If you using Business entities you can find an example here http://msdn.microsoft.com/en-us/library/bb929003.aspx
Hope this helps.
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 12:40 AMModerator -
Correct. An update only requires those attributes that you wish to update. As a corollory, if you are processing Update message via plugins and/or custom workflow activities, only those attributes that were actually updates are passed to your code.
--pogo (pat)- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 3:38 AM
All replies
-
Is my question unclear?
Thanks
Thursday, February 17, 2011 7:39 PM -
You should use the ColumnSet class to retrieve only set of specified attributes.
http://msdn.microsoft.com/en-us/library/bb889187.aspx
http://msdn.microsoft.com/en-us/library/bb929002.aspx
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Proposed as answer by Jim Glass Jr Thursday, February 17, 2011 9:13 PM
- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Thursday, February 17, 2011 8:50 PMModerator -
Thank you for your reply. Retrieving only the relevant columns is no problem, but how can I update the lead in the CRM when my columns are changed?
Thursday, February 17, 2011 11:50 PM -
Something along these lines:
TargetRetrieveLead target = new TargetRetrieveLead(); target.EntityId = new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); RetrieveRequest reqTarget = new RetrieveRequest(); reqTarget.ColumnSet = new ColumnSet(new string[] { "field2", "field3" }); reqTarget.ReturnDynamicEntities = false; reqTarget.Target = target; RetrieveResponse respTarget = (RetrieveResponse)crmService.Execute(reqTarget); lead lead = (lead)respTarget.BusinessEntity; lead.field1 = lead.field2 = lead.field3; crmService.Update(lead);
When updating an existing entity instance, you only need to populate properties for those attributes that you wish to modify. The Primary Key GUID is always populated, even if you don't specify it, in your ColumnSet.
--pogo (pat)- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 12:37 AM -
it depends on if your using dynamic entities or if you have business entitites from a web reference in your project.
Here is examples using a dynamic entity, you'll see an Update example in the code http://msdn.microsoft.com/en-us/library/aa685870.aspx
If you using Business entities you can find an example here http://msdn.microsoft.com/en-us/library/bb929003.aspx
Hope this helps.
MSCRM Bing'd - http://bingsoft.wordpress.com Check out the CRM 4 to CRM 2011 JavaScript Converter Tool CRM Forum Guidance on how to Help Us Help You - Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 12:40 AMModerator -
Thanks, so I query only the columns I need and I can safely do crmService.Update(mylead) without overwriting any other value.
As the client can't provide a test environment, I didn't want to risk something on his data.
Thank you again for your help
Friday, February 18, 2011 2:13 AM -
Correct. An update only requires those attributes that you wish to update. As a corollory, if you are processing Update message via plugins and/or custom workflow activities, only those attributes that were actually updates are passed to your code.
--pogo (pat)- Marked as answer by Donna EdwardsMVP Monday, February 28, 2011 8:40 PM
Friday, February 18, 2011 3:38 AM