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)

Retrive Single

(0) ShareShare
ReportReport
Posted on by 397

Hi,

I am trying to find a method that fetches one record with filter condition. IOrganization service has retrieve and RetrieveMultiple methods.

I know my result set contains only one record but at the same time i dont want to pass the Guid to Retrieve method, i have a where condition that i am evaluating. Is there any thing that i can use to retrieve the record with QueryExpression of fetchXml query ? I cannot pass the QueryExpression or fetchXml to IorganizationService Retrieve method or i just need to rely on RetrieveMultiple method?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Try with this -

                    QueryExpression qe = new QueryExpression();
                    qe.EntityName = "account";
                    qe.ColumnSet = new ColumnSet();
                    qe.ColumnSet.Columns.Add("name");
                    //Add columns 
                    EntityCollection ec = _orgService.RetrieveMultiple(qe);


  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello ,

    Here is sample of with condition query expression retrieve record -

    QueryExpression query = new QueryExpression("contact"); 
    query.ColumnSet.AddColumns("firstname", "lastname", "address1_city"); 
    
    query.Criteria = new FilterExpression(); 
    query.Criteria.AddCondition("address1_city", ConditionOperator.Equal, "Redmond"); 
    
    FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.Or); 
    childFilter.AddCondition("lastname", ConditionOperator.Equal, "Tharpe"); 
    childFilter.AddCondition("lastname", ConditionOperator.Equal, "Brown"); 
    
    // Pass query to service proxy 
    EntityCollection results = _serviceProxy.RetrieveMultiple(query); 
    Console.WriteLine(); 
    Console.WriteLine("Query using QE with multiple conditions and filters"); 
    Console.WriteLine("---------------------------------------"); 
    
    // Print results 
    foreach (var a in results.Entities) 
    { 
    Console.WriteLine("Name: {0} {1}", a.GetAttributeValue<string>("firstname"), a.GetAttributeValue<string>("lastname")); 
    Console.WriteLine("City: {0}", a.GetAttributeValue<string>("address1_city")); 
    } 
    Console.WriteLine("---------------------------------------");


    For more details you can refer below -

    msdn.microsoft.com/.../gg334688.aspx

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi SVN,

    If your condition is to retrieve only one record without passing the Guid of the record you will need to use QueryExpression.

    You can use the results to only get a single/the first record:

    EntityCollection results = service.RetrieveMultiple(query);

    if (results.Entity.Count > 0)

    {

      Entity current = results.Entities[0];

    }

    Hope this helps.

  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi SVN,

    Try following code to retrieve one record with Query Expression.

    Entity results = service.RetrieveMultiple(query).Entities.FirstOrDefault();


    Thanks

    Justin Jose

  • Srini20 Profile Picture
    397 on at

    Hi Goutam,

    Thanks for the reply i was looking for a call that retrieves a single record rather than making a RetrieveMultiple request. Similar to  _orgService.Retrieve(entityname, id, columnset) but this doens't allow to pass QueryExpression or FetchXml query and i dont want to pass a Guid.

  • Suggested answer
    Srini20 Profile Picture
    397 on at

    Thanks Justin, that works.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi , it's by design you can't pass query for specific record.

    To pass query you need to use query or fetchxml in retrievemultiple method.

    You can use FirstOrDefault in entity collection  after retrieve.

    msdn.microsoft.com/.../cc679523(v=vs.90).aspx

  • Temmy Wahyu Raharjo Profile Picture
    2,916 on at

    Hi,

    For your information. You can use TopCount for limiting your QueryExpression as well.

    msdn.microsoft.com/.../microsoft.xrm.sdk.query.queryexpression.topcount.aspx;MSPPError=-2147217396

  • Suggested answer
    Coty Profile Picture
    89 on at

    I know this is old, but just posting for other users.

    The accepted answer, while less code does the same thing the OP is trying not to do.  It doesn't appear you can actually do what the OP wants with CRM methods.  There are options.  You can query the table directly, this is the fastest way to get the data if you are on premise, but unsupported.  Don't do this if you ever plan to go to CRM online.  You can also query a FilteredView, while supported you shouldn't do this if you are ever going to go to CRM online because it is unsupported there.  FilteredViews in my experience are slow though - so test.

    Other supported options to increase performance.   

    1. Use top='1' in your fetch XML.  That way SQL can just stop at the first match.  This works if you do not use an Order By clause.

    2. Add a database index to the column you are querying.  This could help a lot.

    Thanks,

    Coty

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