You need to sign in to do that
Don't have an account?
Patrick G. Brown
Trigger with two updates not working
I have a trigger on Campaigns that is doing two things.
Any help would be most appreciated. Thanks so much.
- When the Campaign IsActive is flipped to FALSE, I am updating related Campaign Members
- 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).
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.
Finished code: