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
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);
}
|improve this answer|||||
  • 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.