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
MOHAMMED AL IMAMMOHAMMED AL IMAM 

when the campaign member is created then the related opportunities should be mapped to the campaign using trigger

Hi all, I'm new to salesforce ,need trigger code for this
Suraj Tripathi 47Suraj Tripathi 47
Hi

Please follow the below code:-
trigger CampaignMemberTrigger on CampaignMember (after insert) {
    {  
  
     Map<id,CampaignMember>   campaignIdmap = new  Map<id,CampaignMember>(); 
     List<CampaignMember> newMembersList = trigger.new;
        
     for(CampaignMember  cm: newMembersList)	
           {  if(!campaignIdmap.containskey(cm.id))
               {    
                  campaignIdmap.put(cm.campaignId,cm);
               }
		   }	
        List<opportunity> oppList = new  List<opportunity>();
       
      for(id n: campaignIdmap.keyset())
        {   integer i=0;
		opportunity op = new opportunity();
		op.name = 'test'+i;
		op.stagename = 'qualification';
		op.closedate = date.today()+1+i;
		op.campaignId = n;
		oppList.add(op);
         i++;
		}	  
		
		insert oppList;
	}
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi
MOHAMMED AL IMAMMOHAMMED AL IMAM
@Suraj Tripathi 47

Thanks for the reply

for my current requirement i need to see the related opportunities of contact. when i add any contact in campaign member i need to see the related opportunities if any, no need to create any new opportunities. I need code for thiis.


Thanks & Regards
Mohamemd Al Imam


 
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Please follow the below code:-
trigger CampaignMemberTrigger on CampaignMember (after insert) {
    {  
        
        Map<id,List<opportunity>>   map1 = new     Map<id,List<opportunity>>(); 
        List<CampaignMember> newMembersList = trigger.new;
        set<id> conSet =new set<id>();
        for(CampaignMember c :newMembersList ){
            conSet.add(c.contactId);
        }
        List<opportunity> oppList = [select id,contactId from opportunity where contactid in: conSet];
        for(opportunity  cm: oppList)	
        {     if(!map1.containsKey(cm.contactId)){
                   
                List<opportunity> conLi = new List<opportunity> ();
                conLi.add(cm);
                map1.put(cm.contactId, conli);
            } 
            else{
                List<opportunity> conL = map1.get(cm.contactId);
                conL.add(cm);
                map1.put(cm.contactId,conl);
            }
        }
    }
}

Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
MOHAMMED AL IMAMMOHAMMED AL IMAM
Still I'm not able to get the output
MOHAMMED AL IMAMMOHAMMED AL IMAM
@Suraj Tripathi 47
Im not able to get the Output. The opportunities is not adding.If you are getting the output ,please the pic.

Thanks & Regards 
Mohammed Al Imam
 
MOHAMMED AL IMAMMOHAMMED AL IMAM
@Suraj Tripathi 47

Yes thanks for the reply.

I can see the mapped Opportunities with System.debug but the mapped values are NOT updating the related list in Campaign. We are getting the map but we aren't inserting it.
Would you be kind enough to try to insert the mapped values so that it will show the related Opportunities when Campaign is viewed.

Thanks & Regards 
Mohammed Al Imam
Suraj Tripathi 47Suraj Tripathi 47
Hi

For debugging:- 
for(Id i: map1.keyset())	
        {     
        system.debug('Contact id- '+i+' list of Opportunity-  '+map1.get(i);
          }
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi

 
MOHAMMED AL IMAMMOHAMMED AL IMAM
@Suraj Tripathi 47

 thanks for the reply.

Actually I'm getting the values in the debbug successfully,But I want to see the opportunities related to contact  in campaign object.Can help me for that.

Thanks & Regards 
Mohammed Al Imam

 
MOHAMMED AL IMAMMOHAMMED AL IMAM
@Suraj Tripathi 47

 I want to add count of the total number of oppoetunities associated with contact in the campaign object ,when the campaign member is created..In the same code ,could  please help me for this.



Thanks & Regards 
Mohammed Al Imam