Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrive Single

Posted on by 395

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

  • Suggested answer
    Coty Profile Picture
    Coty 87 on at
    RE: Retrive Single

    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

  • Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Retrive Single

    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
    gdas Profile Picture
    gdas 50,085 on at
    RE: Retrive Single

    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

  • Suggested answer
    Srini20 Profile Picture
    Srini20 395 on at
    RE: Retrive Single

    Thanks Justin, that works.

  • Srini20 Profile Picture
    Srini20 395 on at
    RE: Retrive Single

    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
    Justinjose Profile Picture
    Justinjose 2,707 on at
    RE: Retrive Single

    Hi SVN,

    Try following code to retrieve one record with Query Expression.

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


    Thanks

    Justin Jose

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Retrive Single

    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
    gdas Profile Picture
    gdas 50,085 on at
    RE: Retrive Single

    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
    gdas Profile Picture
    gdas 50,085 on at
    RE: Retrive Single

    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);


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