Please try below code if you are looking from trigger perspective.
trigger afterAccountInsert on Account(after insert){ //-- collect all account ids List<String> accountIdList = new List<String>(); for(Account acc: Trigger.new) { accountIdList.add(acc.Id); } System.debug('Account Ids:' + accountIdList);
//-- query all contact associated to account List<Contact> accountContactList = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :accountIdList]; System.debug('Contact Ids:' + accountContactList); }
You can get contact list using account id by following code:
public class AccountContact{
public Set<Id> ConactIds{get;set;}
public Set<ID> AccountContact()
{
Account a = new Account();
a.Name = 'test';
insert a;
List<Contact> contactList = [SELECT Id, Name FROM Contact WHERE Contact.AccountId = :a.Id];
ConactIds = new Set<ID>();
for(Contact con: contactList){
ConactIds.add(con.Id);
}
return ConactIds;
}
}
This is a sample code, you should modify according to your requirment.
Hope this will help you. Let me know if it helped or you need any more assistance.
Please mark this is as the solution if it solved your purpose.
trigger afterAccountInsert on Account(after insert){
//-- collect all account ids
List<String> accountIdList = new List<String>();
for(Account acc: Trigger.new)
{
accountIdList.add(acc.Id);
}
System.debug('Account Ids:' + accountIdList);
//-- query all contact associated to account
List<Contact> accountContactList = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :accountIdList];
System.debug('Contact Ids:' + accountContactList);
}
Regards,
Pawan Kumar
You can get contact list using account id by following code:
This is a sample code, you should modify according to your requirment.
Hope this will help you. Let me know if it helped or you need any more assistance.
Please mark this is as the solution if it solved your purpose.
Thanks,
Sohan Raj Gupta
Sohan Raj Gupta
that sample code is not working ..contact-id is not found ..can you help for this requirement..