You need to sign in to do that
Don't have an account?

Basic After Insert trigger not working
I have a trigger that I've been working on for a few days and worked through all of the errors (with a lot of help from this forum). However, it's simply not working and I can't figure out why. This trigger is on Campaign Member and if the Current_Campaign__c value in my Lead is blank, it looks to add the Campaign_Name__c from my Campaign Member to the Current_Campaign__c field on my associated Lead record. I've tried everything I know to do. Here is my code:
I replaced "associatedLead2.Current_Campaign__c = cml.Campaign_Name__c;" with "associatedLead2.Current_Campaign__c = "text";" to make sure it wasn't the formula field that was causing the error. For some reason, my update isn't working. Any ideas?
trigger setLeadStatusandCurrentCampaign on CampaignMember (after insert) { if(Trigger.isAfter){ if(Trigger.isInsert){ /*Set<Id> leadIds = new Set<Id>(); for (CampaignMember cml: Trigger.new){ leadIds.add(cml.LeadId); }*/ Map<Id, Lead> LeadMap2 = new Map<Id, Lead>([SELECT Id, Current_Campaign__c, Status FROM Lead WHERE Id IN :Trigger.newMap.keyset()]); List<Lead> leadToUpdate2 = new List<Lead>(); Lead associatedLead2; //Loop through Campaign Member Leads and look for Current Campaign values for (CampaignMember cml: Trigger.new){ if(LeadMap2.containsKey(cml.LeadId)){ associatedLead2 = LeadMap2.get(cml.LeadId); if(associatedLead2.Current_Campaign__c == NULL) { //This is not working associatedLead2.Status = 'In Campaign'; associatedLead2.Current_Campaign__c = cml.Campaign_Name__c; leadToUpdate2.add(associatedLead2); } }//if }//for update leadToUpdate2; } } }
I replaced "associatedLead2.Current_Campaign__c = cml.Campaign_Name__c;" with "associatedLead2.Current_Campaign__c = "text";" to make sure it wasn't the formula field that was causing the error. For some reason, my update isn't working. Any ideas?
Try the following answer. I did not complied it so may have typos. Let me know if this helps
Please make sure to mark this as the best answer if this help you, as the community need this to be helpful for them