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 

Trigger with two updates not working

I have a trigger on Campaigns that is doing two things.
  1. When the Campaign IsActive is flipped to FALSE, I am updating related Campaign Members
  2. When the Campagin IsActive is flipped to FALSE, I am updating related Leads (Leads are related directly via a custom field called Current_Campaign_ID__c).
When the Campaign is deactivated, my Leads update no problem.  However, nothing happens to my Campaign Members.  If I remove the entire Leads update section, my Campaign Members work just fine.  Can I not do two different updates in one trigger?
 
trigger deactivateCampaignMember on Campaign (after update) {


//HANDLE UPDATING CAMPAIGN MEMBERS RELATED TO CAMPAIGN
    Set<ID> campids = new Set<ID>();
    for(Campaign camp : Trigger.new){
            campIds.add(camp.Id);
    }

    List<Campaign> updatedCampaigns = [SELECT Id, IsActive, (Select Id, Campaign_IsActive__c from CampaignMembers)  FROM Campaign WHERE Id in :campids];
    List<CampaignMember> campMemsToUpdate = new List<CampaignMember>();

    for (Campaign camp : updatedCampaigns){

        for(CampaignMember cm : camp.CampaignMembers){
            if(camp.IsActive == False){
            cm.Campaign_IsActive__c = False;
                if(cm.Campaign_Lead_Status__c == 'In Campaign'){
                    cm.Campaign_Lead_Status__c ='Resting';
                    campMemsToUpdate.add(cm);
                    
                }
            }
        }
    }
    
    
//HANDLE UPDATING LEADS RELATED TO CAMPAIGN    
        Set<ID> campids2 = new Set<ID>();
        for(Campaign camp2 : Trigger.new){
                campIds2.add(camp2.Id);
        }   
        List<Campaign> updatedCampaigns2 = [SELECT Id, IsActive, (Select Id, Current_Campaign__c, Current_Campaign_ID__c, Status from Leads__r)  FROM Campaign WHERE Id in :campids2];
        List<Lead> campLeadsToUpdate = new List<Lead>();
    
        for(Campaign camp2 : updatedCampaigns2){
        
            for(Lead ld : camp2.Leads__r){
                if(camp2.IsActive == False){
                    if(ld.Status == 'In Campaign'){
                        ld.Status = 'Resting';
                        ld.Current_Campaign__c = '';
                        ld.Current_Campaign_ID__c = NULL;
                        campLeadsToUpdate.add(ld);
                    }
                }    
            }
        }
    update campMemsToUpdate;
    update campLeadsToUpdate;
 }

Any help would be most appreciated.  Thanks so much. 
Best Answer chosen by Patrick G. Brown
Patrick G. BrownPatrick G. Brown
I was able to find the error in my trigger and am posting the corrected code for others to see.  I wasn't querying the Campaign_Lead_Status__c in my Campaign Members, thus when trying to update it, it was failing.  There were also a few typos.  The corrected trigger below just successfully worked for 1,500 Campaign Members and Leads.  It fails between 1500 and 2000 that due to too many rows.  To handle more, I'll need to write a batch.

Finished code:
 
trigger deactivateCampaignMember on Campaign (after update) {


//HANDLE UPDATING LEADS RELATED TO CAMPAIGN    
            Set<ID> campids2 = new Set<ID>();
            for(Campaign camp2 : Trigger.new){
                    campIds2.add(camp2.Id);
            }   
            List<Campaign> updatedCampaigns2 = [SELECT Id, IsActive, (Select Id, Current_Campaign__c, Current_Campaign_ID__c, Status from Leads__r)  FROM Campaign WHERE Id in :campids2];
            List<Lead> campLeadsToUpdate = new List<Lead>();
        
            for(Campaign camp2 : updatedCampaigns2){
            
                for(Lead ld : camp2.Leads__r){
                    if(camp2.IsActive == False){
                        ld.Current_Campaign__c = '';
                        ld.Current_Campaign_ID__c = NULL;
                        if(ld.Status == 'In Campaign'){
                            ld.Status = 'Resting';
                            campLeadsToUpdate.add(ld);
                        }
                    }    
                }
            }
    update campLeadsToUpdate;

//HANDLE UPDATING CAMPAIGN MEMBERS RELATED TO CAMPAIGN
    Set<ID> campids = new Set<ID>();
    for(Campaign camp : Trigger.new){
            campIds.add(camp.Id);
    }

    List<Campaign> updatedCampaigns = [SELECT Id, IsActive, (Select Id, Campaign_IsActive__c, Campaign_Lead_Status__c from CampaignMembers)  FROM Campaign WHERE Id in :campids];
    List<CampaignMember> campMemsToUpdate = new List<CampaignMember>();

    for (Campaign camp : updatedCampaigns){

        for(CampaignMember cm : camp.CampaignMembers){
            if(camp.IsActive == False){
            cm.Campaign_IsActive__c = False;
                if(cm.Campaign_Lead_Status__c == 'In Campaign'){
                    cm.Campaign_Lead_Status__c ='Resting';
                    campMemsToUpdate.add(cm);
                    
                }
            }
        }
    }
    update campMemsToUpdate;
    

 }