+ Start a Discussion
sumit dsumit d 

Total number of Campaign Members (Leads only) where Lead Status = RAW

Hi All,
   I have a requirement where i want to calculate total number of Campaign members on Campaign where lead Status = RAW.
I have created a trigger for it and its helper class is given below:- 

public class CampaignMemberTriggerHelper {
    
    public static List<CampaignMember> newCampaignMembers = new List<CampaignMember>();
    public static List<CampaignMember> oldCampaignMembers = new List<CampaignMember>();
    public static Map<Id, CampaignMember> newMapCampaignMembers = new Map<Id, CampaignMember>();
    public static Map<Id, CampaignMember> oldMapCampaignMembers = new Map<Id, CampaignMember>();
 
    public static void updateNewNetleads(){
        Set<Id> campaignsByIds = new Set<Id>();  
        Map<Id,Campaign> campaignMapToUpdate = new Map<Id,Campaign>(); 
        List<Campaign> updateCampaignList = New List<Campaign>();
        
        if(Trigger.isDelete){
            for(CampaignMember cm: oldCampaignMembers){ 
                if(cm.CampaignId != NULL){
                    campaignsByIds.add(cm.CampaignId); 
                }
            }
        }else{
            for(CampaignMember cm: newCampaignMembers){ 
                if(cm.CampaignId != NULL){
                    campaignsByIds.add(cm.CampaignId); 
                }
            }
        }
        
        if(campaignsByIds.size()>0){
            system.debug('test cam list'+campaignsByIds);
            List<AggregateResult> aggrList = new List<AggregateResult>([SELECT Count(Id)ids,CampaignId   
                                                                        FROM CampaignMember 
                                                                        WHERE CampaignId IN:campaignsByIds
                                                                        AND lead.Status ='RAW' 
                                                                        GROUP BY CampaignId]);
            if(!aggrList.isEmpty()){
                for(AggregateResult aggr:aggrList){ 
                          Campaign cmObj = new Campaign();  
                    cmObj.Net_New_Lead__c  = (Decimal)aggr.get('ids');
                        cmObj.Id = (Id)aggr.get('CampaignId');  
                    campaignMapToUpdate.put(cmObj.Id, cmObj);
                    
                }
            } 
        }
            if(campaignMapToUpdate.values().size() > 0 ){
            update campaignMapToUpdate.values();  
        }
    }
}
I have a number field on which the total number of campaign members display
Now when i change the status of lead it does not work. how can i modify it so that when i change the status of lead still it work correct. As of now only when i create or delete a campaign member it works.
How can i modify this so that when i change the status of lead it works at that time too?
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sumit,

You were doing dml operation Lead then you need to write a trigger on Lead object.

Thanks!!
sumit dsumit d
How can i write trigger on lead to update the field of campaign? can you help me out? I did not find any relation on Campaign and lead
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sumit,

Your trigger will be like below.
trigger cmcountupdateLeadstatus on Lead (after update) {

set<id> leadids = new set<id>();

for(Lead Ld: trigger.New){

If(Ld.status!=Null){
leadids.add(Ld.id);
}
}

set<id> CampaignIds = new set<id>();

for(CampaignMember cm:[select id,CampaignId from CampaignMember where Leadid=:leadids]){

If(!CampaignIds.contains(cm.CampaignId)){
CampaignIds.add(cm.CampaignId)
}

}

 Map<Id,Campaign> campaignMapToUpdate = new Map<Id,Campaign>(); 

if(CampaignIds.size()>0){
            system.debug('test cam list'+campaignsByIds);
            List<AggregateResult> aggrList = new List<AggregateResult>([SELECT Count(Id)ids,CampaignId   
                                                                        FROM CampaignMember 
                                                                        WHERE CampaignId IN:campaignsByIds
                                                                        AND lead.Status ='RAW' 
                                                                        GROUP BY CampaignId]);
            if(!aggrList.isEmpty()){
                for(AggregateResult aggr:aggrList){ 
                          Campaign cmObj = new Campaign();  
                    cmObj.Net_New_Lead__c  = (Decimal)aggr.get('ids');
                        cmObj.Id = (Id)aggr.get('CampaignId');  
                    campaignMapToUpdate.put(cmObj.Id, cmObj);
                    
                }
            } 
        }
            if(campaignMapToUpdate.values().size() > 0 ){
            update campaignMapToUpdate.values();  
        }

}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​