locked
How to Update a PickList value and a Lookup value on Contact Form RRS feed

  • Question

  • Hey folks I wanted to
    1) update the Lookup value for a contact record which I have the GUID id for
    2) update a picklist value on a contact for.

    I thought I could just throw the string or convert it to its type and throw it in there but I'm getting that it may need to do more than that.  here is the code I have:

    I think my mistake is that gender is a picklist and should not be a string and the custom entity field on teh form is country.

    Any help here?

     

    public void CreateUserRecord()

    {

     

    string crmServerUrl = ConfigurationManager.AppSettings["CrmSdk.CrmServiceWsdl"];

     

    string orgName = ConfigurationManager.AppSettings["orgName"];

     

    // Set up the CRM Service.

     

    CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

     

    StringProperty lastname = new StringProperty();

     

    StringProperty country = new StringProperty();

     

    StringProperty gender = new StringProperty ();

     

    lastname.Name =

    "lastname";

    lastname.Value =

    "TESTLAStname";

    country.Name =

    "custom_lookupaddress1_countryid";

    country.Value =

    "<Some GUID>";

    gender.Name =

    "gendercode";

    gender.Value.Value =

    Convert.ToInt32("2");

     

    // Create the DynamicEntity object.

     

    DynamicEntity contactEntity = new DynamicEntity();

     

    // Set the name of the entity type.

    contactEntity.Name =

    EntityName.contact.ToString();

     

    // Set the properties of the contact.,

    contactEntity.Properties =

    new Property[] { lastname, country, gender };

     

    // Create the target.

     

    TargetCreateDynamic targetCreate = new TargetCreateDynamic();

    targetCreate.Entity = contactEntity;

     

    // Create the request object.

     

    CreateRequest create = new CreateRequest();

     

    // Set the properties of the request object.

    create.Target = targetCreate;

     

    // Execute the request.

     

    CreateResponse created = (CreateResponse)service.Execute(create);

    }

    Friday, March 12, 2010 9:29 PM

Answers

  • you can achieve the same like this

    convert

    PicklistProperty gender = new PicklistProperty("gendercode", new Picklist(Convert.ToInt32("2")));

    into

    PicklistProperty gender = new PicklistProperty();
    gender.Value = new PickList();
    gender.Value.Value = 2;

    do the same for all other properties.

    Muhammad Ali Khan
    My MS CRM blog
    • Marked as answer by yaf23 Monday, March 15, 2010 2:35 PM
    • Unmarked as answer by yaf23 Monday, March 15, 2010 3:28 PM
    • Marked as answer by yaf23 Monday, March 15, 2010 3:29 PM
    Monday, March 15, 2010 2:27 PM

All replies

  • I am asuming that "lastname" is a string type, "gendercode" is a Picklist type and "custom_lookupaddress1_countryid" is a lookup type

    public void CreateUserRecord()
            {
                string crmServerUrl = ConfigurationManager.AppSettings["CrmSdk.CrmServiceWsdl"];
                string orgName = ConfigurationManager.AppSettings["orgName"];
                // Set up the CRM Service.
                CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
                StringProperty lastname = new StringProperty("lastname", "TESTLAStname");
                //custom_lookupaddress1_countryid <<< Lookup field name
                //custom_lookupaddress1_country  <<< Custom entity schame name
                LookupProperty country = new LookupProperty("custom_lookupaddress1_countryid", new Lookup("custom_lookupaddress1_country", new Guid("")));
                PicklistProperty gender = new PicklistProperty("gendercode",new Picklist(Convert.ToInt32("2")));
                // Create the DynamicEntity object.
                DynamicEntity contactEntity = new DynamicEntity();
                // Set the name of the entity type.
                contactEntity.Name =EntityName.contact.ToString();
                // Set the properties of the contact.,
                contactEntity.Properties =new Property[] { lastname, country, gender };
                // Create the target.
                TargetCreateDynamic targetCreate = new TargetCreateDynamic();
                targetCreate.Entity = contactEntity;
                // Create the request object.
                CreateRequest create = new CreateRequest();
                // Set the properties of the request object.
                create.Target = targetCreate;
                // Execute the request.
                CreateResponse created = (CreateResponse)service.Execute(create);
            }
    http://www.allaboutdynamics.com
    • Marked as answer by yaf23 Monday, March 15, 2010 1:48 PM
    • Unmarked as answer by yaf23 Monday, March 15, 2010 2:17 PM
    Saturday, March 13, 2010 4:36 AM
  • Hi,
    In addition to the shahid response above on how to use Picklist & Lookup. I belive you are updating the record?
    In this case  you have to use TargetUpdateDynamic instead of TargetCreateDynamic

    TargetUpdateDynamic target = new TargetUpdateDynamic ();
    target.Entity= contactEntity;

    UpdateRequest update = new UpdateRequest();
    update.Target = target;
      

    UpdateResponse updated = (UpdateResponse)service.Execute(update);
    Muhammad Ali Khan
    My MS CRM blog
    Saturday, March 13, 2010 5:53 AM
  • Well Ali is right, is case of Update your code will looks like

    public void UpdateUserRecord()
            {
                string crmServerUrl = ConfigurationManager.AppSettings["CrmSdk.CrmServiceWsdl"];
                string orgName = ConfigurationManager.AppSettings["orgName"];
                // Set up the CRM Service.
                CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
                StringProperty lastname = new StringProperty("lastname", "TESTLAStname");
                //custom_lookupaddress1_countryid <<< Lookup field name
                //custom_lookupaddress1_country  <<< Custom entity schame name
                LookupProperty country = new LookupProperty("custom_lookupaddress1_countryid", new Lookup("custom_lookupaddress1_country", new Guid("")));
                PicklistProperty gender = new PicklistProperty("gendercode", new Picklist(Convert.ToInt32("2")));
                KeyProperty id = new KeyProperty("contactid", new Key(new Guid(""))); //<<< Guid of the Record you want to edit
                // Create the DynamicEntity object.
                DynamicEntity contactEntity = new DynamicEntity();
               
                // Set the name of the entity type.
                contactEntity.Name = EntityName.contact.ToString();
                // Set the properties of the contact.,
                contactEntity.Properties = new Property[] {id, lastname, country, gender };
                // Create the target.
                TargetUpdateDynamic targetUpdate = new TargetUpdateDynamic();
                targetUpdate.Entity = contactEntity;
                // Create the request object.
                UpdateRequest update = new UpdateRequest();
                // Set the properties of the request object.
                update.Target = targetUpdate;
                // Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);
            }


    http://www.allaboutdynamics.com
    Saturday, March 13, 2010 6:44 AM
  • Thank you both I actually will need both create and update. 
    Monday, March 15, 2010 1:49 PM
  • I tried this out and it worked if I were to use the DLL but I prob should have mentioned that I am using the webservice directly so the error I get is that

    Crm.Sdk.LookupProperty does not contain a constructor that takes 2 arguements.  Is there a way around this or a simialr way to achieve this using the Web service 4.0?

    I experience the same issue for the PickLists as well.  Any help?

    Monday, March 15, 2010 2:19 PM
  • you can achieve the same like this

    convert

    PicklistProperty gender = new PicklistProperty("gendercode", new Picklist(Convert.ToInt32("2")));

    into

    PicklistProperty gender = new PicklistProperty();
    gender.Value = new PickList();
    gender.Value.Value = 2;

    do the same for all other properties.

    Muhammad Ali Khan
    My MS CRM blog
    • Marked as answer by yaf23 Monday, March 15, 2010 2:35 PM
    • Unmarked as answer by yaf23 Monday, March 15, 2010 3:28 PM
    • Marked as answer by yaf23 Monday, March 15, 2010 3:29 PM
    Monday, March 15, 2010 2:27 PM