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

PawanKumarPawanKumar
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);
}

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

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