Skip to main content

Notifications

Dynamics 365 general forum
Answered

(PLUGIN/C#) Can we use Retrieve method to retrieve a record with a QueryExpression ?

Posted on by 15

Hi all, really fast question !

I have managed to get a filtered record by using RetrieveMultiple and by specifying my filtering criteria in a QueryExpression/FilterExpression.

It's working fine but I have the feeling that it must be a cleaner solution to archive this !

I don't need to retrieve more than one record, I just need to confirm that a record with value "x" in field "y" exist in my DB.

Can I do that with a simple Retrieve() ? 

Or in other words, what's the most proper way to do what I want to do ?

Thanks for help!

  • DynaMoX Profile Picture
    DynaMoX 15 on at
    RE: (PLUGIN/C#) Can we use Retrieve method to retrieve a record with a QueryExpression ?

    Ok, so even with FetchXML I still need to declare an EntityCollection to get my record...

    Thank you for your help !

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: (PLUGIN/C#) Can we use Retrieve method to retrieve a record with a QueryExpression ?

    in your scenario FetchXML is equivalent to a QueryExpression, with C# the code will be:

    service.RetrieveMultiple(new FetchExpression(fetchXMLquery));

    I hope you can find a "secret joker" but the platform for this component is the same in the last 13 years. You can try some tricks if the field can be an alternate key (so unique) and doing a retrieve by the alternate key.

  • DynaMoX Profile Picture
    DynaMoX 15 on at
    RE: (PLUGIN/C#) Can we use Retrieve method to retrieve a record with a QueryExpression ?

    I keep it open to see if eventually somebody have a "secret joker", but thank you anyway for your help! I will mark your answer as a solution if nobody can "find better" !

    PS: A friend have said to me that I could use directly a FetchXML in my code but we didn't had the time to go into details, if somebody know what he means and have an example, please don't wait for a special invitation before sharing your knowledge ;) Thanks

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: (PLUGIN/C#) Can we use Retrieve method to retrieve a record with a QueryExpression ?

    if you need to check by a field value and not by ID, RetrieveMultiple is the only way.

    Retrieve requires the ID and it throws an exception if the record is not found, from this point doing a RetrieveMultiple is also better because you check the result Entities count (0, 1 or more than 1)

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,469 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans