+ Start a Discussion
majomajo 

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!

 

 

 

Message Edited by majo on 10-16-2009 08:52 AM
Message Edited by majo on 10-16-2009 08:53 AM
Best Answer chosen by Admin (Salesforce Developers) 
AKallAKall
I would try with something like this.

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

AKallAKall
I would try with something like this.

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; }

 

This was selected as the best answer
majomajo

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?

AKallAKall

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

Message Edited by AKall on 10-15-2009 01:13 PM
majomajo

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

AKallAKall
Add the task object as a related list to your Opportunity page layout. When you create a new Oppty you should see the task in the related list.
majomajo

Ok, and how can I do that?

(sorry but I'm new at sf)

AKallAKall
Edit the Opportunity page layout. You can chose the related list that you want to appear. There is a shortcut to the layout editor in the upper right hand corner of all opportunity records. Or you can find in the Set Up menu.
majomajo

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.

majomajo

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!!!

 

Rahul BhallaRahul Bhalla
This worked for me

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;
}
dalveer singhdalveer singh

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;
}

 

Balamurali Sayimpu 4Balamurali Sayimpu 4
Hi Dalveer,
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; 
}
 
Sonali GajjarSonali Gajjar
Hi Everyone!
Please try below code for best practise
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
  
    List<Task> taskList=new List<Task>();
    
    for(opportunity o:trigger.new)
    {
        if(o.StageName=='Closed Won')
        {
            tasklist.add(new Task(Subject = 'Follow Up Test Task',OwnerId = UserInfo.getUserId(),Status = 'New',Type = 'Call',WhatId=o.id));  
        }
    }

  if(taskList.size()>0)      
  {
      insert tasklist;
  }
        
}

Thanks 
Sonali Gajjar