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 

LIMIT 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

Best Answer chosen by VenkataRaja
Mariappan PerumalMariappan Perumal

Hi

 

List<Account> acc=[Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.];

 

The size of the above limit may be above 5 since it contains record too but it will show exactly 5 when you try this

 

 

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

 

Try to process contacts of each account with 2 for loops

 

Kindly let me know incase you need more assistance on this .

 

All Answers

Puja_mfsiPuja_mfsi

Hi,

Yes u can use LIMIT keyword in subquery,But in your query there is one another problem,U wrote the wrong childRelationShip name of contact to Account.

The chidRelatiobShipname is 'Contacts' NOT contact.

 

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

 

otherwise the query is perfect .

 

Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Venkat,

 

This is possible, because you are selecting all the contacts records in the query so there may be more than a record. So, you can limit only fist 5 should be queried after ordering it.

 

But a slight change is there, you need to mention the subquery object's name in plural.

 

So your query look like,

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

 

Hope this helps you...!

VenkataRajaVenkataRaja

Hi All,

 

Thanks for reply. Yes I know that contacts. But I am getting soem wrong result. I am getting Contact List is 8 but limit is 5.

May I know why It is.

 

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

Mariappan PerumalMariappan Perumal

Hi

 

List<Account> acc=[Select id, Name, (Select id,name,CreatedDate from Contact ORDER BY CreatedDate LIMIT 5) from Account.];

 

The size of the above limit may be above 5 since it contains record too but it will show exactly 5 when you try this

 

 

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

 

Try to process contacts of each account with 2 for loops

 

Kindly let me know incase you need more assistance on this .

 

This was selected as the best answer
VenkataRajaVenkataRaja

HI,

   Thanks for your reply. Actually I miss understood that query. What ever your are saying that is correct

 

Thanks & Regards

Venkat