Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How check for multiple values in an entityReference field

Posted on by 390

Hi Guys,

  I am faced with checking for multiple values in a lookup field, something like the below:

 EntityReference trustRef = clinic.GetAttributeValue<EntityReference>("new_trust");
                    
                    if (trustRef.Name.Trim() == "North  Trust", "walsall", "shewsbury".......)

I Know there is an efficient way to check for this condition. Thanks for the help

*This post is locked for comments

  • Suggested answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: How check for multiple values in an entityReference field

    What I meant is adding another bool attribute (isPrivileged) in entity so that you can mark that as true for records like North Trust, Walsall and false for others.

    In future if more records added in there or Walsall has to be removed from this check does not need a code change & redeployment.

    Simply that bool can be turned off & code will retrieve the records which has value true for that & check dynamically in code.

    Otherwise my initial code check will work fine.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: How check for multiple values in an entityReference field

    @Goutam, the entity contains all other values (the users can enter multiple values), but I want to check for these specific string values to perform an operation, so creating another entity would conflict.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: How check for multiple values in an entityReference field

    Also I would suggest for best practice  keep those hard code value in an CRM entity (with  Key, Value fields) and retrieve from the entity using key inside plugin instead of hard code. In future if those value chagnes you need to again change your plugin code .

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How check for multiple values in an entityReference field

    No worries Victor. I don't think you a re junior developer now, you have already written so many plugins :)

    Regarding this questions, if you sure that the name will not change then you can use or as suggested above.

    if (trustRef.Name.Trim() == "North  Trust" || trustRef.Name.Trim() == "walsall" || trustRef.Name.Trim() == "shewsbury" || .......)

    Hope this helps. Remember to close the thread once the issue is fixed.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: How check for multiple values in an entityReference field

    it is a requirement given to us by the client, i believe the name would strictly spelt, that is why i use the trim() method. Hi Ravi, sorry for bugging you,I am pretty new at developing in dynamic CRM (junior developer), can you look at the issue i am also facing ? community.dynamics.com/.../290894 Thanks  

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How check for multiple values in an entityReference field

    btw you are tagging wrong Ravi :)

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: How check for multiple values in an entityReference field

    Hi Victor,

    It is not a good idea to check for the text/name value of the record. What if someone change the name from "North Trust" to "Norther Trust". You should either check the GUID

    What's the actual requirement you have? I even think that you should not be checking the records at all.

  • Victor Onyebuchi Profile Picture
    Victor Onyebuchi 390 on at
    RE: How check for multiple values in an entityReference field

    Hi Arun, I havent used isPreviledged on CRM before, really do not know what it is.

  • Verified answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: How check for multiple values in an entityReference field

    You have to check like this:

    if (trustRef.Name.Trim() == "North  Trust" || trustRef.Name.Trim() == "walsall" || trustRef.Name.Trim() == "shewsbury" || .......)

    This is ok to certain level, but not a good practice as these are entity references which are entity records, so people may go & rename it. If you think your code should not break, then keep some flag like isPrivileged and mark as true for these records. Then you can query & verify whether the trust lookup in clinic record matches one of that isPrivileged List then proceed.

  • Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: How check for multiple values in an entityReference field

    Did you edit the question with totally different content?

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans