In the Force.com Explorer I can perform the following query:
SELECT Id, (SELECT Id,LeadId,ContactId FROM CampaignMembers) FROM Campaign

It displays every campaign.Id in one column and the next column has a list of campaignmembers associated with the campaign.

If my example code below is incorrect please feel free to correct it. I am still new to apex coding.


I need to create a Campaign map that is as follows where first parameter of Map is CampaignId:

Map<Id, List<Id,CampaignMember>> newMap = new Map<Id, List<Id, CampaignMember>>();
List<Id> cntList = new List<Id>;
List<Id> leadList = new List<Id>;

for(CampaignMember mem: memberList){
   if(mem.LeadId != null )
     else if (mem.ContactId != null)

  List<CampaignMember> newCms = new List<CampaignMember>();
   //assuming new map is correct format and is filled in
   For(Campaign cmp : campList) {
     List<CampaignMember> cmMems = newMap.get(cmp.Id);

//check if lead/contact is a campaign member, if not make the lead a campaign member
//the lead/contact records below are just to show you that i am getting a lead or contact associated with campaign members

Lead ld1 = [Select Id From Lead Where Id IN: leadList Limit 1];
Contact cnt1 = [Select Id From Contact Where Id IN: cntList Limit 1];
if(ld1 != null) 
       //leads is not associated with campaign member so it will be added
       if( !(cmMems.contains(ld)) )
            CampaignMember newCm = new CampaignMember(CampaignId=cmp.Id, LeadId=ld1.Id);
else if (cnt1 != null)
       //contact is not assocaited with a campaign member so it will be added
       if( !(cmMems.contains(cnt1)) )
            CampaignMember newCm = new CampaignMember(CampaignId=cmp.Id, ContactId=cnt1.Id);

  }//end for

Create a map of Id and list<campaignMemebers>.

Map<id, list<campaignMembers>> campaignMemberMap = new Map<Id, List<CampaignMemebers>>();

//iterate thru all the campaigns and get the associated campaign memebers
for(campaign camp : [SELECT Id, (SELECT Id,LeadId,ContactId FROM CampaignMembers) FROM Campaign]){
    //get the list of campaign member.
    list<campaignMembers> campMemebersList = camp.campaignMember;
    if(campMemebersList!= null){
        campaignMemberMap.put(camp.Id, campMemebersList);

Now that you have the map, you can get the values from the map using the .get() method, but before that always check if the value exits in the map using the .containsKey method to avoid nullpointerexception.

    List<campaignMembers> tempList  = campaignMembersMap.get(someCampignId);
  • I know this is old, but incase others see this, your solution is correct except that when referring to the Child records after the Query you still need to use the relationship name. So. list<campaignMembers> campMemebersList = camp.campaignMember; should be list<campaignMembers> campMemebersList = camp.campaignMembers;
    – Minutiae
    Nov 5 '14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.