0

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.

snap

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 )
     leadList.add(mem.LeadId)
     else if (mem.ContactId != null)
      cntList.add(mem.ContactId);
  }

  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);
            newCms.add(newCm); 
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);
            newCms.add(newCm); 

  }//end for

1 Answer 1

2

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.

if(campaignMemberMap.containsKey(someCampignId)){
    List<campaignMembers> tempList  = campaignMembersMap.get(someCampignId);
}
1
  • 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
    Commented Nov 5, 2014 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .