You need to sign in to do that
Don't have an account?
Patrick 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?
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; } } }
User below code,
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