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 EastEdward East 

Update Task Status when Lead Status Changes

I am trying to write a trigger that will automatically mark a task called 'Lead - Intro' as complete when the Lead Status changes from Open to Contacted. I tried cobbling it together from various similiar things I've found on the forum, but there are big chunks missing and I've sort of hit a brick wall. This is how far I've come so far:

trigger updateTaskStatus on Lead (after update) {
 
try{   
        for(Lead myLead: Trigger.new){
            
            if((myLead.isconverted==false) && (myLead.Status == 'Contacted')) {
            
        List<Task> taskstatus = new List<Task>();
        
             for (Task t: taskstatus){
  if (t.Subject == 'Lead - Intro') && (t.Status == 'Open') {
    t.Status = 'Completed';
  }
            
                Update taskstatus;                             
                }
Best Answer chosen by Edward East
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi Edward,

Try below code sample
trigger updateTaskStatus on Lead(after update)
{
	set < id > setLeadID = new set < id > ();
	for (Lead myLead: Trigger.new)
	{
		if ((myLead.isconverted == false) && (myLead.Status == 'Contacted'))
		{
			setLeadID.add(myLead.id);
		}
	}
	list < task > lstTask = [select, id, subject, status from task where whoid in : myLead];
	list < task > lsTaskForUpdate = new list < task > ();
	for (task objtask: lstTask)
	{
		if (objtask.Subject == 'Lead - Intro') && (objtask.Status == 'Open'))
		{
			lsTaskForUpdate.add(objtask);
		}
	}
	if (lsTaskForUpdate.size() > 0)
	{
		update lsTaskForUpdate;
	}
}

Regards
Ankit Gupta