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
Deepanshu KocharDeepanshu Kochar 

Error while creating "Add a task" trigger on opportunity

Hi All , i created trigger on Opportunity to add a follow up task whenever a new opportunity is created with "StageName = Closed Won". --->

trigger ClosedOpportunityTrigger on Opportunity (after insert , after update) {

list<task> listtask = new list<task>();
for (opportunity opp : trigger.new){

if (opp.StageName == 'Closed Won'){
task NewTask = new task();
task.subject = 'Follow Up Test Task';
task.ActivityDate = system.today().addmonths(1);
task.whatid = opp.Id;
listtask.add(NewTask);
}


}
if (listtask.size()>0)
insert listtask;


}


How ever while saving the trigger i get an error : Error: Compile Error: Expression cannot be assigned at line -1 column -1

Can not seem to move past this , please help also point out if you see any rookie mistakes in the trigger code.

Thanks
Best Answer chosen by Deepanshu Kochar
rajat Maheshwari 6rajat Maheshwari 6

Hi Deepanshu,

Use this code, It will help you :)

trigger ClosedOpportunityTrigger on Opportunity (after insert , after update) 
{

list<task> listtask = new list<task>();
for (opportunity opp : trigger.new)
{
if (opp.StageName == 'Closed Won')
{
task NewTask = new task();
NewTask.subject = 'Follow Up Test Task';
NewTask.ActivityDate = system.today().addmonths(1);
NewTask.whatid = opp.Id;
listtask.add(NewTask);
}


}
if (listtask.size()>0)
insert listtask;

}




Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Deepanshu,

Use this code, It will help you :)

trigger ClosedOpportunityTrigger on Opportunity (after insert , after update) 
{

list<task> listtask = new list<task>();
for (opportunity opp : trigger.new)
{
if (opp.StageName == 'Closed Won')
{
task NewTask = new task();
NewTask.subject = 'Follow Up Test Task';
NewTask.ActivityDate = system.today().addmonths(1);
NewTask.whatid = opp.Id;
listtask.add(NewTask);
}


}
if (listtask.size()>0)
insert listtask;

}




Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
This was selected as the best answer
Deepanshu KocharDeepanshu Kochar
Oh damn ,  I was'nt using the new instance "NewTask" to add fields ... Such a rookie blunder .
Thanks'
rajat Maheshwari 6rajat Maheshwari 6
Please mark as best answer :)

Thanks
Rajat maheshwari
rajatzmaheshwari@gmail.com