2

i have declared a set of id's. I initially added 2 different account id's to set as below

 Set<id> acctSet = new Set<id>();
 acctset.add('001c000000g0ijo');
 acctset.add('001c000000g0iFJ');
 system.debug('***acctSet:'+acctset);

Now for each of the above Accounts, they have 2 to 3 contacts respectively. Now i am querying them.

 List<Contact> Cons = new List<Contact>([select id, AccountId from Contact where accountid IN :   acctSet ORDER BY CreatedDate ASC NULLS LAST LIMIT 1]);
system.debug('***cons:'+cons);

Now the above results shows only one account and its related contact(As i Limited result to 1). is this the correct behavior of sets or am i missing something. My expected output is like it should get related first created contact for both accounts.

11

The limit of 1 limits the results to 1 record TOTAL not one per account

If you want 1 per account your query would have to be

[Select ID, (select id, AccountId from Contacts ORDER BY CreatedDate ASC LIMIT 1) From Account where ID IN :   acctSet];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.