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
Edward Scott 5Edward Scott 5 

how to update a field based on a lookup field

I am trying to write a trigger that will update a picklist field on the lead object. So when a lead has an activity added such as a call or email I would like for the lead status picklist to be updated from Not Contacted/New to Contacted. Thank for your help.
Best Answer chosen by Edward Scott 5
Abhishek BansalAbhishek Bansal
Hi Edward,

Please use the below trigger code to achieve what you want :
trigger updateLeadStatus on Task(after insert, after update){
	Set<Id> leadIds = new Set<Id>();
	
	for(Task tsk : trigger.new){
		if(String.valueOf(tsk.WhoId).startsWith('00Q')){
			leadIds.add(tsk.WhoId);
		}
	}
	
	List<Lead> updateLeadList = new List<Lead>();
	for(Lead ld : [Select status from Lead where Id in :leadIds]){
		ld.status = 'Contracted'; //Use any other value as per your requirement
		updateLeadList.add(ld);
	}
	
	if(updateLeadList.size() > 0){
		update updateLeadList;
	}
}

Let me know if you have any issue with the above trigger code.

Thanks,
Abhishek

All Answers

Vipin  PulinholyVipin Pulinholy
Write a trigger on Task and check if the WhoId starts with '00Q'. That means this Task is assocated with Lead and then you can update the the corresponding Lead Record.

Hope this helps.
Edward Scott 5Edward Scott 5
So should I write two triggers one on task and one for the lead record? 
Abhishek BansalAbhishek Bansal
Hi Edward,

Please use the below trigger code to achieve what you want :
trigger updateLeadStatus on Task(after insert, after update){
	Set<Id> leadIds = new Set<Id>();
	
	for(Task tsk : trigger.new){
		if(String.valueOf(tsk.WhoId).startsWith('00Q')){
			leadIds.add(tsk.WhoId);
		}
	}
	
	List<Lead> updateLeadList = new List<Lead>();
	for(Lead ld : [Select status from Lead where Id in :leadIds]){
		ld.status = 'Contracted'; //Use any other value as per your requirement
		updateLeadList.add(ld);
	}
	
	if(updateLeadList.size() > 0){
		update updateLeadList;
	}
}

Let me know if you have any issue with the above trigger code.

Thanks,
Abhishek
This was selected as the best answer
Edward Scott 5Edward Scott 5
Thanks Abhishek I appreciate your help. I am about to try it out now.
Edward Scott 5Edward Scott 5
The trigger worked perfectly. Thanks again. I was also wondering if there is anyway to exclude leads that we have coming in from Wed-to-lead. When the lead comes in from the web it is created by a profile we named SB Marketing and the type of the task is marked as other. I have been trying to make it so that either the profile name or the task type can be used to not update the lead status? Thanks
Edward Scott 5Edward Scott 5
I actually figured it out thanks.
askmikemaskmikem
To improve the user experience, is there a way to update the status while the user is still on the screen?  ie, can we update the feild in real time without having to wait for the save action?