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

Notifications

Announcements

No record found.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans