You need to sign in to do that
Don't have an account?
majo
Trigger a new Task when creating an Opportunity's product
Hello,
I'm new at developing in salesforce.com , so I'm having a little trubble to finding out some things.
First of all, I would like to write a trigger for the Opportunity. I want to create automatically a new task, when ever a user creates a new Opportunity.
trigger createTask on Opportunity (before insert, before update){
List<Task> task = new List<Task>();
for (Integer i = 0; i < Trigger.new.size(); i++) {
task.add(new Task(
whatid=Trigger.new[i].Id,
OwnerId=Trigger.new[i].OwnerId,
Subject='First Contact '
) ) ;
}
insert task;
}
My idea for this trigger, is to create a task for each new Opportunity created. Is it right?
thanks!
trigger createTask on Opportunity (before insert, before update){ List<Task> tasks = new List<Task>(); List<Opportunity> Opps = Trigger.new; for (Opportunity Opp : Opps) { Task tsk = new Task(whatID = Opp.ID, Ownerid = Opp.OwnerId); tasks.add(tsk); } insert tasks; }
All Answers
trigger createTask on Opportunity (before insert, before update){ List<Task> tasks = new List<Task>(); List<Opportunity> Opps = Trigger.new; for (Opportunity Opp : Opps) { Task tsk = new Task(whatID = Opp.ID, Ownerid = Opp.OwnerId); tasks.add(tsk); } insert tasks; }
There is something weird because the trigger that I have created , actually worked but not so good. Then I tried the one you wrote me , but It didn't generate any task. Finally, I've tested my trigger again, and now it doesn't work.
Any clue?
I just tested my trigger, and it seems to work fine. Can you give me some more detail on how mine didnt work? Did you receive any error messages?
-Andy
Ok, let me tell what I have done step by step
1) Config >> Add setup >> Customize >> Opportunity >> Apex Trigge
2) New and I paste your code
3) New Opportunity >> complete basic fields >> save and add product
4) When I have to select a product, click on cancel
5) done
And then when I check the task, there are no new ones. I have the task in the main page, where else I can see them? Thanks
Ok, and how can I do that?
(sorry but I'm new at sf)
The layout for the opportunities is showing the activities and the history of all activitis, so the task should be there.
Only 4 task created by the trigger are showing in one opportunity, the one I've created yesterday. But after that, It stopped working.
I FOUND THE PROBLEM!
When I edited the trigger, the checkbox "active" had been unchecked automatically! So the trigger wasnt running :smileymad:A
Thaaaaaaaaaaaaanks so much for your help and patience Akall!!!
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
LIST<Task> tasks = new LIST<Task>();
for( Opportunity Opp: [select Id from Opportunity where
Id in :Trigger.New AND StageName = 'Closed Won'])
{
tasks.add(new Task(whatID = Opp.ID, Subject='Follow Up Test Task'));
}
insert tasks;
}
Hello,
Can u pls check my code and let me now where the issue is.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
LIST<Task> tasks = new LIST<Task>();
list<Opportunity> opplist = [select id from Opportunity where id IN:trigger.new];
for(Opportunity o:opplist){
if(o.StageName == 'Closed Won'){
tasks.add(new Task(whatID = o.id, Subject='Follow Up Test Task'));
}
}
insert tasks;
}
Please select StageName in select query.
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
LIST<Task> tasks = new LIST<Task>();
List<Opportunity> relatedOpps = [SELECT Id,StageName FROM Opportunity
WHERE Id IN :Trigger.New];
for(Opportunity opp : relatedOpps) {
if(opp.StageName == 'Closed Won'){
tasks.add(new Task(whatID = opp.id, Subject='Follow Up Test Task'));
}
}
insert tasks;
}
Please try below code for best practise
Thanks
Sonali Gajjar