CRM 4 Web Service: How to update only one field RRS feed

  • Question

  • Hello fellow developpers,

    I query a CRM entity with all fields every time I plan to update that entity. Then I do:


    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!


    Thursday, February 17, 2011 5:06 PM


All replies

  • Is my question unclear?



    Thursday, February 17, 2011 7:39 PM
  • You should use the ColumnSet class to retrieve only set of specified attributes.



    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 PM
  • 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;

    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)
    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
    Friday, February 18, 2011 12:40 AM
  • 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)
    Friday, February 18, 2011 3:38 AM