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
bujjibujji 

How to get Contact Ids List from Account List

Hi,

I have a account list (aList), i need contact ids list from the account list. How to do it. 
Here i am getting 10 accounts, suppose if each account having 10 contacts than i want all the 100 contact ids in one list.
How to write the loop for this.Below is the code where i am getting all the 10 accounts and 100 contacts in one list.


String baseQuery = 'Select Id,Name,(select Id,Name from Contacts Limit 10) from Account ';
       
String finalQuery = baseQuery +' where Id IN (select AccountId from Contact) Order By Name limit 10';

List<Account> aList = Database.Query(finalQuery);

Please give me suggestions how to get contacts ids list.

Thanks,
Bujji

Best Answer chosen by bujji
srlawr uksrlawr uk
You should totally just be able to pass the alist into the second query?

Something like this works:

List<Account> alist = [SELECT Id, Name FROM Account];

System.debug('a' + alist);

List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];

System.debug('c' + clist);

So you see I just did "Contact.AccountId IN :alist" and this loads all the contacts in the list from above (which in my case is every account)

After you have constructed your alist (which you have in your question) just use the query I did (with your own field/selectors).

List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];




All Answers

Vatsal KothariVatsal Kothari
Hi,

Yo can do like this:
String finalQuery = 'Select Id,Name from Account limit 10';

List<Account> aList = Database.Query(finalQuery);

List<Contact> conList = [Select Id,FirstName,LastName,AccountId from Contact where AccountId IN: aList];
conList contains all the contacts.

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
srlawr uksrlawr uk
You should totally just be able to pass the alist into the second query?

Something like this works:

List<Account> alist = [SELECT Id, Name FROM Account];

System.debug('a' + alist);

List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];

System.debug('c' + clist);

So you see I just did "Contact.AccountId IN :alist" and this loads all the contacts in the list from above (which in my case is every account)

After you have constructed your alist (which you have in your question) just use the query I did (with your own field/selectors).

List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];




This was selected as the best answer
Vatsal KothariVatsal Kothari
Loop for all the contact List:
String finalQuery = 'Select Id,Name from Account limit 10';

List<Account> aList = Database.Query(finalQuery);

List<Contact> conList = [Select Id,FirstName,LastName,AccountId from Contact where AccountId IN: aList];

for(Contact con : conList){
	System.debug('Contact ID: 'con.Id);
	System.debug('Contact FirstName: 'con.FirstName);
}


bujjibujji
Thanks for reply, it help me a lot. 
I have one more scenario like, i have a list which is of type ID, it contains contact ids but i want to assign these to some campaign how to do it.
Give me some suggestion.

Thanks,
Bujji
srlawr uksrlawr uk
For that, you want to create "CampaignMember" records.... you will need to load your campaign via SOQL (in the code below I call this theLoadedId) and then do something like

List<CampaignMember> newCMs = new List<CampaignMember>();
for(thisId Id : idList)
{
    newCMs.add(new CampaignMember(Campaign = theLoadedId, ContactId = Id, Status = 'Sent'));
}
insert newCMs;
or to that effect (I'm speed pseudo-coding here!). That will insert new campaignMember records for those contacts.

You can read more on the CampaignMember object here https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_campaignmember.htm
bujjibujji
Thanks Again, but here we have a situation like suppose if the Contact is already a member of that campaign we no need to add but if Contact is not a member than we need to add to the campagin. Because here we are doing 'Insert'.

Do we need to change any thing.

Thanks,
Bujji
bujjibujji
Is it possible to find the non-assigned contact ids to that campaign from the list

Thanks,
Bujji
Prashant MaxsteelPrashant Maxsteel
Hi Folks,
A slight difference in the single query
Account sAcc = [Select Id, Name, (Select Id, FirstName from Contacts) Where Id =:sId Limit 1];

Here, how do we access the Contact details from sAcc using dot notation?

 
sfdc freshersfdc fresher
hi srlawr uk,

will it considering account names or account ids...I tried to use below

List<Contact> clist = [SELECT Id, Name,AccountID  FROM Contact WHERE Contact.AccountId IN :alist]

but  am getting Account name instead of AcountID.
Could anyone please help me in this.
Andrew S DayAndrew S Day
@sfdc fresher, 

This is from two years ago but looking at your code the first AccountID call has an uppercase D.  Hope that helps.