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
Patrick G. BrownPatrick G. Brown 

Two trigger variables inside one for loop

I have an interesting trigger on Campaign Member that needs to check a value on the associated Lead and associated Campaign and if both are TRUE, it should fire and update a few fields.  The challenge I'm having is that I can't figure out how to get both the Campaign and Lead variables inside of one for loop, without creating a loop inside of a loop, which will inevitably give me a DML insert error.  I create two Sets to get the IDs of the respective Leads and Campaigns, however, when I try to add the Campaign inside of my for loop I'm receiving a "variable doesn't exist" error.  I understand why...it's because I'm creating a "for"' on cml and not cmc (see below).  

I've commented out the code that is giving me issues.  How do I loop through two different objects that are related by a m2m (Campaign Member), relate them to each other, check for values in both Lead and Campaign, then update my Lead?  Any suggestions?
 
trigger deactivateCampaignSetToResting on CampaignMember (before Update) {
if(Trigger.IsBefore){
    if(Trigger.IsUpdate){
        Set<Id> leadIds = new Set<Id>();
        for (CampaignMember cml: Trigger.new){
            leadIds.add(cml.LeadId);
        }
        Set<Id> campaignIds = new Set<Id>();
        for (CampaignMember cmc: Trigger.New){
            campaignIds.add(cmc.CampaignId);
        }
        Map<Id, Lead> LeadMap2 = 
            new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status 
                              FROM Lead 
                              WHERE Id IN :LeadIDs]);
        List<Lead> leadToUpdate2 = new List<Lead>();
        Lead associatedLead2;
        
        Map<Id, Campaign> CampaignMap = 
            new Map<Id, Campaign>([SELECT Id, IsActive
                                    FROM Campaign
                                    WHERE Id IN :CampaignIds]);
        
        for (CampaignMember cml: Trigger.new){
            if(LeadMap2.containsKey(cml.LeadId)){
                associatedLead2 = LeadMap2.get(cml.LeadId);
                //if(CampaignMap.containsKey(cmc.LeadId)){
                //associatedCampaign = CampaignMap.get(cmc.CampaignId);    
                //    if( Trigger.oldMap.get(cmc.Id).IsActive != Trigger.newMap.get(cmc.Id).IsActive ){
                        if(associatedLead2.Current_Campaign__c!=NULL){
                            if(cml.Campaign_Lead_Status__c !='Pre-Contracting'){
                                if(cml.Campaign_Lead_Status__c !='Contracting'){
                                    if(cml.Campaign_Lead_Status__c !='Disqualified'){
                                        
                                        cml.Campaign_Lead_Status__c = 'Resting';
                                        associatedLead2.Status = 'Resting';
                                        associatedLead2.Current_Campaign__c = '';
                                        leadToUpdate2.add(associatedLead2);
                                    }
                                }
                            }
                            
                        } 
                        
                    //}
                    
                //}
                
            }
            
        }
        update leadToUpdate2;
    }
}
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

User below code, 
 
trigger deactivateCampaignSetToResting on CampaignMember (before Update) {
if(Trigger.IsBefore){
    if(Trigger.IsUpdate){
        Set<Id> leadIds = new Set<Id>();
        for (CampaignMember cml: Trigger.new){
            leadIds.add(cml.LeadId);
        }
        Set<Id> campaignIds = new Set<Id>();
        for (CampaignMember cmc: Trigger.New){
            campaignIds.add(cmc.CampaignId);
        }
        Map<Id, Lead> LeadMap2 = 
            new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status 
                              FROM Lead 
                              WHERE Id IN :LeadIDs]);
        List<Lead> leadToUpdate2 = new List<Lead>();
        Lead associatedLead2;
        
        Map<Id, Campaign> CampaignMap = 
            new Map<Id, Campaign>([SELECT Id, IsActive
                                    FROM Campaign
                                    WHERE Id IN :CampaignIds]);
        
        for (CampaignMember cml: Trigger.new){
            if(LeadMap2.containsKey(cml.LeadId)){
                associatedLead2 = LeadMap2.get(cml.LeadId);
                if(CampaignMap.containsKey(cml.LeadId)){
              Campaign  associatedCampaign = CampaignMap.get(cmc.CampaignId);    
                    if( Trigger.oldMap.get(cml.Id).IsActive != Trigger.newMap.get(cml.Id).IsActive ){
                        if(associatedLead2.Current_Campaign__c!=NULL){
                            if(cml.Campaign_Lead_Status__c !='Pre-Contracting'){
                                if(cml.Campaign_Lead_Status__c !='Contracting'){
                                    if(cml.Campaign_Lead_Status__c !='Disqualified'){
                                        
                                        cml.Campaign_Lead_Status__c = 'Resting';
                                        associatedLead2.Status = 'Resting';
                                        associatedLead2.Current_Campaign__c = '';
                                        leadToUpdate2.add(associatedLead2);
                                    }
                                }
                            }
                            
                        } 
                        
                    }
                    
                }
                
            }
            
        }
        update leadToUpdate2;
    }
}
}

Note : if you are getting this error  " Error: Compile Error: Variable does not exist: IsActive at line 29 column 52" there is field called  IsActive in CampaignMember  try to map it campaign isActive. 

Thanks
karthik
 
Patrick G. BrownPatrick G. Brown
Karthik, I truly appreciate your response.  Unfortunately, I'm receiving the error you described and there doesn't seem to be an IsActive field on Campaign Member.  It would be much easier that way as I'd not have to query Campaigns...I'll continue to work on it.  Thanks for taking the time to look at my issue.