function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VenkataRajaVenkataRaja 

LMIT key word in subquery.

Hi All

 

              I am tryign to use like this. Can I Use this.

 

Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.

 

How Can I use Limit With in the SubQuery Is it possible.

 

Regards

Venkat

Satish_SFDCSatish_SFDC
Yes, you can use the query you mentioned, but with a small change.
Instead of Contact in the subquery, give the plural form - Contacts.

Select id, Name, (Select id,name,CreatedDate from Contacts ORDER BY CreatedDate LIMIT 5) from Account

Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
VenkataRajaVenkataRaja

Hi Satish,

   Thanks for your reply. Yes you are correct that is Contacts. But In reusult LIMIT key word and Order By clause is not working. It is showing more contacts reocords not 5. but I mentioned limit 5 only.

 

Regards

Venkat

VenkataRajaVenkataRaja

HI ALL

 

             Set<Id> accIds = new Set<Id>();

              List<Account> accList = [select id from Account];

             for(Account accRec : accList){

                        accIds.add(accRec.Id);

             }

            List<Contact> conList = [select id,name,CreatedDate from Contact Where AccountId In : accIds order by CreatedDate DESC Limit 5];

 

System.debug('======conList======'+conList+'===='+conList.Size());

 

Like this I am getting Correct Result.

Plese reply If it is correct process. Other give some simple query.

 

Regards

Venkat