suppose custom entity contains lookup field how will map them into MS CRM 2011 Using C#?

suppose custom entity contains lookup field how will map them into MS CRM 2011 Using C#?

This question has suggested answer(s)

I am creating a custom web application capturing contact information(using ASP.net and C#), then updating the information into CRM on- Premise. And I am able to Update all the information into CRM 2011 Successfully expect Lookup Attribute value So could you please let me know if suppose custom entity contains lookup field how will map them into MS CRM 2011?

All Replies
  • You need an entity reference

    Microsoft.Xrm.Sdk.AttributeCollection attributeList = new Microsoft.Xrm.Sdk.AttributeCollection();

    EntityReference fieldreference = new EntityReference();
    fieldreference.Id = TheIDOfTheLookupRecordGoesHere;
    fieldreference.LogicalName = TheNameOfTheLookupEntityGoesHere;
    attributeList.Add(TheNameOfTheLookupFieldGoesHere, fieldreference);

    For Example:
    Microsoft.Xrm.Sdk.AttributeCollection attributeList = new Microsoft.Xrm.Sdk.AttributeCollection();

    EntityReference fieldreference = new EntityReference();
    fieldreference.Id = new Guid("E3B6702F-3EE5-E211-BF68-1CC1DEEB47C0");
    fieldreference.LogicalName = "Contact";
    attributeList.Add("ContactLookup", fieldreference);

  • Provided that you have the Guid and the logical entity name of the record you want to associate with the custom entity via the lookup - you simply need to use an EntityReference:

    target["attributename"] = new EntityReference("account", myAccountId);

    Scott Durow

    Read my Blog   |  Follow Me on Twitter  |  Rockstar365

  • Hi,

    How did you get on with this? Did you get it working?

    Cheers!

    Scott Durow

    Read my Blog   |  Follow Me on Twitter  |  Rockstar365