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 campaign member is created its related opportunity should be counted and once that campaign member is removed that its related opportunity count should be decreased.


Change the below code trigger to helper class and provide test class also
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); } else{ oppList = map1.get(cm.contactId); oppList.add(cm); map1.put(cm.contactId,oppList); } } 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; } }
 
PriyaPriya (Salesforce Developers) 

Hi Mohammed,

Can you please provide the code in proper format so that it becomes easy for us to read.

regards,

Priya Ranjan

MOHAMMED AL IMAMMOHAMMED AL IMAM
@priya
When campaign member is created its related opportunity should be counted and once that campaign member is removed that its related opportunity count should be decreased.

    Yes I'm reposting the code 

trigger campaigntrigger on CampaignMember (before 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);  
            
        }             
        else{              
            oppList = map1.get(cm.contactId); 
            oppList.add(cm);   
            map1.put(cm.contactId,oppList);
          
        }
    }
    
    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;
    }
}