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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieving Guid of a lookup field

(0) ShareShare
ReportReport
Posted on by 60

I am looking for a c# code to get the GUID of a lookup field.  This should be a generic code to accommodate any entity. 

Following is the scenario.

Need to create a lead  from a external web form. In web form  Nationality is provided as Text.   This text needs to be translated to   GUID  to add lookup field in the lead form.  

What I need a function which  receive parameter Lookup table, lookupGUIDcolumnName, and LookupTextColumnName.

eg: Lookup table = crm_nationality

     lookupGUIDcolumnName= crm_nationalityId

     LookupTextColumnName = crm_nationalityText

Any help is greatly appreciated. 

                   

*This post is locked for comments

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: Retrieving Guid of a lookup field

    You can do this using QueryByAttribute to get the GUID of a record based on matching on a field value. It would look something like this:

    You'll then need to check how many records are returned - ideally it would be one, but you should handle the scenarios of 0 records (no match), or multiple records (duplicates). If duplicates are possible, you can use the Orders property to specify the order

    QueryByAttribute qa = new QueryByAttribute(LookupTable);
    qa.ColumnSet = new ColumnSet(LookupTextColumnName, LookupGUIDcolumnName);
    //  Attribute to query.
    qa.Attributes.AddRange(LookupTextColumnName);
    //  Value of queried attribute to return.
    qa.Values.AddRange(LookupTextValue);
    EntityCollection ec = _service.RetrieveMultiple(qa);


  • jma728 Profile Picture
    60 on at
    RE: Retrieving Guid of a lookup field

    Hi David,

    The code you provided is worked well for me.  Thanks a lot.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans