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 

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:
 
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?
Ruwantha  LankathilakaRuwantha Lankathilaka
It seems you have a problem in the query

Try the following answer. I did not complied it so may have typos. Let me know if this helps

 
trigger setLeadStatusandCurrentCampaign on CampaignMember (after insert) {
	if(Trigger.isAfter){
		if(Trigger.isInsert){
			
			Map<Id,Lead> idLeadsMap= new Map<id,Lead>([SELECT Id, Name, Phone FROM Lead WHERE Id IN ( SELECT LeadId FROM CampaignMember WHERE Id IN :Trigger.new)]);
			Lead associatedLead;
			List<Lead> leadsToUpdate = new List<Lead>();
			for (CampaignMember cm: Trigger.new){
				if(idLeadsMap.containsKey(cm.LeadId) && idLeadsMap.get(cm.LeadId).Current_Campaign__c == NULL){
					associatedLead = idLeadsMap.get(cm.LeadId);
					associatedLead.Status = 'In Campaign';
					associatedLead.Current_Campaign__c = cm.Campaign_Name__c;
					leadsToUpdate.add(associatedLead);
				}
			}
			update leadToUpdate;
		}
	}
}


Please make sure to mark this as the best answer if this help you, as the community need this to be helpful for them