locked
Get Record in Entity Where Field Equals A Value RRS feed

  • Question

  • How do i retrieve a record in an entity where a field is of a certian value, so i can then populate a lookup in another entity with this record.
    Tuesday, August 6, 2013 12:48 PM

All replies

  • First, you need to create a QueryExpression:

    //  Query using ConditionExpression
      ConditionExpression condition1 = new ConditionExpression();
      condition1.AttributeName = "lastname";
      condition1.Operator = ConditionOperator.Equal;
      condition1.Values.Add("Brown");
                    
    
      FilterExpression filter1 = new FilterExpression();
      filter1.Conditions.Add(condition1);
    
      QueryExpression query = new QueryExpression("contact");
      query.ColumnSet.AddColumns("firstname", "lastname");
      query.Criteria.AddFilter(filter1);
    
      EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);

    This will retrieve all the records where lastname equals "Brown", and store the results in an EntityCollection object. 

    You then need to retrieve the (correct) value from the entitycollection and then populate the lookup

    Entity retrievedEntity = result1[0];
    
    //This will create an account entity and add the retrieved entity ID to the primarycontact lookup field
    
    Entity account= new Entity("account");
    
    //Missing: add other required attributes
    
    EntityReference primaryContactId = new EntityReference("contact", contactId);
    account["primarycontactid"] = retrievedEntity.ID;
    
    //Create the new account entity
    
    _serviceProxy.Create(account)

    Tuesday, August 6, 2013 1:05 PM