locked
How to set a lookup value in CRM 2011 using C# RRS feed

  • Question

  • I want to assign a value to an attribute that is of type Lookup. How to do that in MS CRM 2011? The examples on web show only JavaScript, however the namespaces used in Javascript are not available in C#. Am I missing something?

     

    Thanks


    .Net Developer
    Tuesday, February 15, 2011 2:16 PM

Answers

  • Hi, maybe I wasn't clear enough,

    This is the snipt that may work for you:

    //First is created a new contact

    Entity contact = new Entity("contact");
    contact.Attributes["firstname"] = "ContactFirstName";
    contact.Attributes["lastname"] = "ContactLastName";
    Guid contactId = _orgService.Create(contact, null);

    //Then assign the contact to an account.

     Entity account = new Entity("account");
    account["name"] = "Test Account1";
    EntityReference primaryContactId = new EntityReference("contact", contactId);
    account["primarycontactid"] = primaryContactId;

    Cheers.


    Ricardo Barona CRM Consultant
    Wednesday, February 16, 2011 5:36 PM

All replies

  • Hi,

    Maybe this cuold help. Next code works for Retrieve Multiple with Query Expression, but at the very first lines of code you can see how a contact is created and then an Account, you can see how the primary contact value is assigned.

    static void RetrieveMultipleWithRelatedEntityColumns()
    {
     Console.WriteLine("Entering:RetrieveMultipleWithRelatedEntityColumns");
     //Create multiple accounts with primary contacts
     Entity contact = new Entity("contact");
     contact.Attributes["firstname"] = "ContactFirstName";
     contact.Attributes["lastname"] = "ContactLastName";
     Guid contactId = _orgService.Create(contact, null);
    
     Entity account = new Entity("account");
     account["name"] = "Test Account1";
     EntityReference primaryContactId = new EntityReference("contact", contactId);
     account["primarycontactid"] = primaryContactId;
     Guid accountId1 = _orgService.Create(account, null);
     account["name"] = "Test Account2";
     Guid accountId2 = _orgService.Create(account, null);
     account["name"] = "Test Account3";
     Guid accountId3 = _orgService.Create(account, null);
    
     //Create a query expression specifying the link entity alias and the columns of the link entity that you want to return
     QueryExpression qe = new QueryExpression();
     qe.EntityName = "account";
     qe.ColumnSet = new ColumnSet();
     qe.ColumnSet.Columns.Add("name");
    
     qe.LinkEntities.Add(new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner));
     qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname");
     qe.LinkEntities[0].EntityAlias = "primarycontact";
    
     EntityCollection ec = _orgService.RetrieveMultiple(qe);
    
     Console.WriteLine("Retrieved {0} entities", ec.Entities.Count);
     foreach (Entity act in ec.Entities)
     {
      Console.WriteLine("account name:" + act["name"]);
      Console.WriteLine("primary contact first name:" + act["primarycontact.firstname"]);
      Console.WriteLine("primary contact last name:" + act["primarycontact.lastname"]);
     }
    }
    
    

    Let me know if this works for you.

    Cheers.


    Ricardo Barona CRM Consultant
    • Edited by Ricardo Barona Tuesday, February 15, 2011 3:19 PM HTML Format modified code.
    • Proposed as answer by Ricardo Barona Tuesday, February 15, 2011 10:41 PM
    Tuesday, February 15, 2011 3:17 PM
  • Hi, maybe I wasn't clear enough,

    This is the snipt that may work for you:

    //First is created a new contact

    Entity contact = new Entity("contact");
    contact.Attributes["firstname"] = "ContactFirstName";
    contact.Attributes["lastname"] = "ContactLastName";
    Guid contactId = _orgService.Create(contact, null);

    //Then assign the contact to an account.

     Entity account = new Entity("account");
    account["name"] = "Test Account1";
    EntityReference primaryContactId = new EntityReference("contact", contactId);
    account["primarycontactid"] = primaryContactId;

    Cheers.


    Ricardo Barona CRM Consultant
    Wednesday, February 16, 2011 5:36 PM
  • Hi,

    can you please provide some inputs regarding how to set the look up to blann in CRM 2011.

    -Mitesh

    Tuesday, June 21, 2011 2:03 PM
  • Hello,

    Currently I'm facing a similar problem. I'm developing an asp.net application to integrate with CRM and I need to allow users to set the value of the contact parent account by entering its name

    Or by selecting the account from a dialog that is similar to the lookup dialog

    Please advise,

    Monday, May 7, 2012 3:13 PM
  • it is showing an error near      "_orgservice .Create(contact,null)"
    Wednesday, February 20, 2013 11:34 AM
  • Hi,

    To assign a value to the lookup field pass the default guid value of the related enttiy.

    Monday, March 4, 2013 12:39 PM
  • You need to make sure that you have a service context available and/or that it is called _orgservice
    Tuesday, July 2, 2013 6:13 AM