Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrieving Guid of a lookup field

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

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

    Hi David,

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

  • Verified answer
    David Jennaway Profile Picture
    David Jennaway 14,063 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);


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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans