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 

trigger the count the total no of opportunities associated with contact in campaign object,when campaign member is created ?

In this code:

trigger  campaigntrigger  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,Name,StageName 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);
                 
            }for(Id i: map1.keyset())    
        {     
        system.debug('Contact id- '+i+' list of Opportunity-  '+map1.get(i));
          }
         
        }
   
          }
 
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>>(); 
     Map<id,integer>   map2 = new   Map<id,integer>(); 
        List<CampaignMember> newMembersList = trigger.new;
        set<id> conIdSet =new set<id>();
     set<id> campIdSet =new set<id>();
        for(CampaignMember c :newMembersList ){
            conIdSet.add(c.contactid);
            campIdSet.add(c.campaignId);
        }
        List<opportunity> oppList = [select id,contactId,Name,StageName from opportunity where campaignId in: campIdSet and contactId in: conIdSet];
        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);
                 
            }
         
         for(Id i: map1.keyset())    
        {     
       Map2.put(i, map1.get(i).size());
          }
         
         for(Id i: map2.keyset())    
        {     
        system.debug('Contact id- '+i+' Total 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

Hi  can you provide the test class for below code:

trigger campaigntrigger on CampaignMember (after insert) { Map<id,List<opportunity>> map1 = new Map<id,List<opportunity>>(); set<id> conSet =new set<id>(); for(CampaignMember c :trigger.new ){ conSet.add(c.contactId); } for(opportunity cm: [select id,contactId,Name,StageName from opportunity where contactid in: conSet]){ List<opportunity> oppList; if(!map1.containsKey(cm.contactId)){ oppList = new List<opportunity> (); oppList.add(cm); map1.put(cm.contactId, oppList); system.debug('success' +map1); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); system.debug('fail' +map1); } } List<Opportunity> oppsToUpdate = new List<Opportunity>(); for(CampaignMember cm : trigger.new){ if(cm.ContactId != NULL){ if(map1.get(cm.ContactId) != NULL){ for(Opportunity op : map1.get(cm.ContactId)){ op.CampaignId = cm.CampaignId; oppsToUpdate.add(op); } } } } if(oppsToUpdate.size() > 0){ update oppsToUpdate; } }

Thanks & Regards
MOHAMMED AL IMAM
Suraj Tripathi 47Suraj Tripathi 47
Hi,
 
@istest
public class campaigntriggerTest {
    @istest
    public static void testTrigger(){
        
        Account acc = new account();
        acc.name = 'testing';
        insert acc;
        
        Contact con = new contact();
        con.lastname = 'testCon';
        con.AccountId = acc.id;
        insert con;
        
         Contact con1 = new contact();
        con1.lastname = 'testCon1';
        con1.AccountId = acc.id;
        insert con1;
        Campaign camp = new Campaign(
            Name = 'Test',
            IsActive = TRUE
        );            
        insert camp;
       
        Opportunity op = new opportunity();
        Op.name='testOpp';
        Op.StageName='Qualified';
        Op.CloseDate = date.today();
        Op.ContactId =con1.id;
        insert op;
        
        List<Opportunity> oppList = new List<Opportunity>();
        
        For(integer i=0;i<2; i++){
        Opportunity Opp1 = new opportunity();
        Opp1.name='tetsOpp'+i;
        Opp1.StageName='Qualified';
        Opp1.CloseDate = date.today()+1+i;
        Opp1.ContactId =con.id;
        oppList.add(Opp1);
        }
        insert oppList;
        
        
        List<CampaignMember> members = new List<CampaignMember>();
        CampaignMember member = new CampaignMember(
            ContactId = con.Id,
            Status = 'sent',
            CampaignId = camp.Id
            
        ); 
        members.add(member); 
        
        CampaignMember member2 = new CampaignMember(
            ContactId = con.Id,
            Status = 'sent',
            CampaignId = camp.Id
        ); 
        
        members.add(member2); 
        insert members;
       
    }
}
Please mark it as Best Answer if it helps you.

Thanks & Regards
Suraj Tripathi