locked
Dynamically updating an entity in MS CRM RRS feed

  • Question

  • Hi, trying to update an entity dynamically.

    For most field types this works, however for a few fields I get an exception:      

    AttributeCollection ac = new AttributeCollection();

                    Entity ent = new Entity();
                    ent.LogicalName = "opportunity";
                    ent.Id = id;    //id = the opportunity guid

                   ac.Add("firstname", "Jim"); //success

                   ac.Add("mypicklistfield", 1); //


                    IOrganizationService _service = (IOrganizationService)_orgServiceProxy;
                    ent.Attributes = ac;
                    _service.Update(ent);

    Anyone have a definitive list of casts etc that needs to be made for different types of MS CRM fields when dynamically updating the entity?

    Thanks in adv.

    Wednesday, November 28, 2012 12:59 PM

Answers

  • Hi,

    try to use below code - 

    AttributeCollection ac = new AttributeCollection();

                    Entity ent = new Entity();
                    ent.LogicalName = "opportunity";
                    ent.Id = id;    //id = the opportunity guid

                   ac.Add("firstname", "Jim"); //success

                   ac.Add("mypicklistfield",new OptionSetValue(1)); //

                    IOrganizationService _service = (IOrganizationService)_orgServiceProxy;
                    ent.Attributes = ac;
                    _service.Update(ent);


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!
    Vikram !


    • Edited by _Vikram Wednesday, November 28, 2012 1:03 PM
    • Marked as answer by CRM001 Wednesday, January 2, 2013 8:40 PM
    Wednesday, November 28, 2012 1:02 PM

All replies

  • Hi,

    try to use below code - 

    AttributeCollection ac = new AttributeCollection();

                    Entity ent = new Entity();
                    ent.LogicalName = "opportunity";
                    ent.Id = id;    //id = the opportunity guid

                   ac.Add("firstname", "Jim"); //success

                   ac.Add("mypicklistfield",new OptionSetValue(1)); //

                    IOrganizationService _service = (IOrganizationService)_orgServiceProxy;
                    ent.Attributes = ac;
                    _service.Update(ent);


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!
    Vikram !


    • Edited by _Vikram Wednesday, November 28, 2012 1:03 PM
    • Marked as answer by CRM001 Wednesday, January 2, 2013 8:40 PM
    Wednesday, November 28, 2012 1:02 PM
  • Hi Vikram,

    Thanks, that does help.

    I'm also looking for other examples, for example, how would you go about updating the statuscode and date time values.

    Wednesday, November 28, 2012 2:49 PM
  • Hi,

    for updating date time field

      ac.Add("datefiled", new DateTime(2012,11,28));

    for updating status code you need to write SetState request check the following link -

    http://social.microsoft.com/Forums/is/crmdevelopment/thread/f7259f2a-f3d3-488b-841f-b5496ea50d9d


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful !!!
    Vikram !

    Wednesday, November 28, 2012 3:12 PM
  • hi

    check with below code

    // for status

                        SetStateRequest setStateReq = new SetStateRequest();
                        setStateReq.EntityMoniker = new EntityReference("contact",ContactID);
                        setStateReq.State = new OptionSetValue(1);
                        setStateReq.Status = new OptionSetValue(-1);
    
                        SetStateResponse response = (SetStateResponse)service.Execute(setStateReq);

    // for date

    ent["yourDateTimeField"]=new DateTime().Date; // for Present Day


    Please don't forget to Vote and marked as answer If this post answers your question or was helpful, please click "Mark As Answer" on the post and "Mark as Helpful" Be wise

    Wednesday, November 28, 2012 3:31 PM