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
Sunil Kumar 545Sunil Kumar 545 

If account is insert ,it will return contact id using apex class?can anyone help this requirement..

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:
    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);

Pawan Kumar
Sunil Kumar 545Sunil Kumar 545
In apex class how have to do ?this requirement..
Sohan Raj GuptaSohan Raj Gupta
Hi Sunil,

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){
        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.

Sohan Raj Gupta 
Sunil Kumar 545Sunil Kumar 545
Sohan Raj Gupta 
              that sample code is not working is not found ..can you  help for this requirement..                
Sunil Kumar 545Sunil Kumar 545
this requirement code not working id is not getting ..can anyone help this requirement?