+ Start a Discussion
Karthikeyan ChandranKarthikeyan Chandran 

How to get list of Account records?

Hi Team,

I want to get all the list of Account records having Campaignmembers assosiate with Campaign Name "Lead Campaign".

Can anyone help me to query all the campaignmembers account records?

Thanks
AnkaiahAnkaiah (Salesforce Developers) 
Hi Karthikeyan,

try with below query.
select accountid,Account.Name from contact where id IN (select contactId from Campaignmember where Campaign.name='Lead Campaign' )
If this helps, Please mark it as best answer.

Thanks!!
 
ravi soniravi soni
hi Karthikeyan,
try below code and find your solution.
map<string,Campaignmember> accountIdWiseCampaignmemberMap = new map<string,Campaignmember>();

    for(Contact con : [SELECT Id,Name,accountid,(SELECT contactId from Campaignmembers where Campaign.name='Lead Campaign' ) 
                       FROM Contact Where accountId != null]){
                           if(con.Campaignmembers != null){
         accountIdWiseCampaignmemberMap.put(con.accountid,con.Campaignmember);                                                 
                           }
         
        
    }

system.debug('accountIdWiseCampaignmemberMap========> ' + accountIdWiseCampaignmemberMap);

list<Account> lstAccount = [SELECT Id,Name FROM Account Where Id IN : accountIdWiseCampaignmemberMap.keyset()];
system.debug('lstAccount========> ' + lstAccount);

for(Account acc : lstAccount){
if(accountIdWiseCampaignmemberMap.containskey(acc.Id)){
Campaignmember oCampaignmember = accountIdWiseCampaignmemberMap.get(acc.Id);
system.debug('oCampaignmember===> ' + oCampaignmember);
}
}

don't forget to mark it as the best answer.
Thank you