web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to add Lookup Value to form field using C#

(0) ShareShare
ReportReport
Posted on by

Hello,

I am trying to add a value to a lookup field on a CRM phone, via C# code.

The lookup field value already exists - I just want to populate the CRM form field (linked to the lookup) with this value.

The lookup is a custom entity with just one field and I know the value of the field but not the GUID.

Does anyone know how I can add it without knowing the GUID, using something like EntityReference??

Many thanks,

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Seamus,

    If you're trying to set the value of a lookup, you need to have the ID of the record. You'll be able to get the ID by querying for the record where the Field Value equals your known Value. Your query should get back a single result (with the ID), and you'll set that on the lookup. An easy way to query is using the RetrieveMultiple request - msdn.microsoft.com/.../microsoft.xrm.sdk.iorganizationservice.retrievemultiple.aspx

    Hope this helps! I'd appreciate if you'd mark this as a Verified answer.

    Thanks,

     Aiden

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Just FYI one of EntityReference's properties - Id with type of Guid. You have to have Guid to update a lookup field.

    If you have value of a primary entity field you can get id by field name using RetreiveMultiple operation.

  • Seamus Profile Picture
    1,577 on at

    Thanks for this. This is for an import so rather than make 1000s of retrievemultiple calls would it be a viable alternative to populate a list with the lookup field values and do a linq query on the list using the (unique) attribute value?

  • Seamus Profile Picture
    1,577 on at

    I would like to mark this as verified answer but for the hell of me I cant see how to!???

  • Suggested answer
    Seamus Profile Picture
    1,577 on at

    Thanks again

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Seamus,

    If you can do a single query to get back all your values and filter that, that would be a lot faster/better than making a thousand calls for the same data.  To mark an answer verified, look at the posts you'd like to approve and on the right side of the post there should be a question "Was this post helpful?" or something, Yes/No. If you select yes, it will show as a Verified answer.

    Thanks again!

     Aiden

  • Seamus Profile Picture
    1,577 on at

    Access Denied

    You do not have permissions to perform the requested action.

    when I try to verify answer!! I will keep trying!  

  • Saddamk206 Profile Picture
    777 on at

    Get Value of Lookup fields

    EntityReference entref = (EntityReference)item.Attributes[attributeName];

    var LookupId = entref.Id;

    var logicalName = entref.LogicalName;

    Set Value of Lookup fields

    newAccount[attributeName] = new EntityReference(logicalName, LookupId);

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans