You need to sign in to do that
Don't have an account?
Narasimha Lakshmi
Getting this error when writing the following trigger,May i know where i did mistake.....
trigger ClosedOpportunityTrigger on Opportunity (before insert,before update) {
List<task> tsklst=new List<task>();
List<Opportunity> opplst=new List<Opportunity>([select id,StageName from opportunity where id in:Trigger.New]);
//List<Opportunity> opplst=new List<Opportunity>();
if(Trigger.isInsert || Trigger.isUpdate)
{
if(opplst.StageName =='Closed Won')
{
for(Opportunity opp:Trigger.new){
system.debug('@@@@@@@' +opp);
Task t=New Task();
t.WhatId = opp.id;
t.Subject = 'Follow Up Test Task';
tsklst.add(t);
}
}
insert tsklst;
}
}
List<task> tsklst=new List<task>();
List<Opportunity> opplst=new List<Opportunity>([select id,StageName from opportunity where id in:Trigger.New]);
//List<Opportunity> opplst=new List<Opportunity>();
if(Trigger.isInsert || Trigger.isUpdate)
{
if(opplst.StageName =='Closed Won')
{
for(Opportunity opp:Trigger.new){
system.debug('@@@@@@@' +opp);
Task t=New Task();
t.WhatId = opp.id;
t.Subject = 'Follow Up Test Task';
tsklst.add(t);
}
}
insert tsklst;
}
}
Hi Narasimha,
I did not quite get your error from your post, but my guess is that your list of Opportunities is empty.
Also your logic is messed up.
Problems with your code:
To get the Opportunities with the IDs of the records that entered the Trigger, you would have to pass the IDs of those records.
Trigger.New will return List<Opportunities> and ''WHERE ID IN : Trigger.New" would not make any sense. It would expect IDs and you are passing records. To make this work you would have to pass the IDs of those records.
After that you are trying to access StageName of a record, but passing the List.
Here either you must specify which record you want to get, the first would be:
Then you would iterate through the Trigger New.
This is problem with the code, however there is problems with your logic.
The main is, why would you query the records that are inside the trigger, when you have them in Trigger.New?
The code would probably look something like this: