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
randombardrandombard 

For each loop to close tasks on opportunity

Hi All,

 

Little stuck on this one in the following code how do I first add another variable to the SOQL to only include Tasks with the Subject Line 'Opportunity Past Close Date' and then go through the list as a for each loop closing them.

 

The code I have right now will just close one and I can tell that's because of the line

 

TasksToClose[0].Status = 'Completed';}

 

But I can't work out how to make that an integer

 

trigger Close_Tasks on Opportunity (after insert, after update) 
{
for(Opportunity o: trigger.new)
{
    If((o.stagename=='Closed Won'))
        {List<Task> TasksToClose = new List<Task>();
        {TasksToClose = [select Id,WhatID,Status from Task where WhatID=:o.Id]; TasksToClose[0].Status = 'Completed';}
        update  TasksToClose;
        }
    else
{
}
}
}

 

Thanks

R

 

Best Answer chosen by Admin (Salesforce Developers) 
randombardrandombard

In Case anyone else has this issue the following seems to work for me

 

trigger Close_Tasks on Opportunity (after insert, after update) 
{
for(Opportunity o: trigger.new)
{
    If((o.stagename=='Closed Won'))
        {List<Task> TasksToClose = new List<Task>();
        {For(task t:[select Id,WhatID,Status from Task 
        where WhatID=:o.Id AND Subject Like 'Opportunity Past Close Date'])
        {
         t.Status = 'Completed';
        TasksToClose.add(t);
        }
        update  TasksToClose;
        }
        }
    else
{
}
}
}

 

All Answers

Naidu PothiniNaidu Pothini
trigger Close_Tasks on Opportunity (after insert, after update) 
{
	List<Id> OppIds = new List<MId>();
	
	for(Opportunity o: trigger.new)
	{
		If(o.stagename=='Closed Won')
		{
			OppIds.add(o.Id);
		}
	}
	
	List<Task> TasksToClose = new List<Task>();
	
	for(Task t:[select Id,WhatID,Status from Task where WhatID IN :OppIds AND Subject Like \'Opportunity Past Close Date\'])
	{
		t.Status = 'Completed';
		TasksToClose.add(t);
	}
	
	updatde TasksToClose;
}

 Try this.

randombardrandombard

Hello and thanks for the help.

 

I get the following error

 

Error

Error: Compile Error: line breaks not allowed in string literals at line 15 column -1

randombardrandombard

If I change the code to as follows

 

 

    for(Task t:[select Id,WhatID,Status from Task where 
    WhatID IN :OppIds AND Subject = 'Opportunity Past Close Date'])

 

 

I get:

 

Error: Compile Error: Duplicate variable: TasksToClose (attempt to re-create the variable with type: updatde) at line 22 column 13

 

This appears to be in reference to:


 

    updatde TasksToClose;

 


randombardrandombard

In Case anyone else has this issue the following seems to work for me

 

trigger Close_Tasks on Opportunity (after insert, after update) 
{
for(Opportunity o: trigger.new)
{
    If((o.stagename=='Closed Won'))
        {List<Task> TasksToClose = new List<Task>();
        {For(task t:[select Id,WhatID,Status from Task 
        where WhatID=:o.Id AND Subject Like 'Opportunity Past Close Date'])
        {
         t.Status = 'Completed';
        TasksToClose.add(t);
        }
        update  TasksToClose;
        }
        }
    else
{
}
}
}

 

This was selected as the best answer