+ Start a Discussion
tedevangtedevang 

Create quick task with opportunity

 I'm trying to create a task in the same screen as the opportunity using the trigger below. When I use this in the Lead object it works fine but on the opportunity object it doesn't create the task nor gives me an error. Can anyone see where my error may lie?

 

Thx.

 

trigger taskCreator on Opportunity (before insert, before update, after insert) {
  if (Trigger.isBefore) { 
    Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>();


 } else if (Trigger.isAfter)  { 
    // after leads are inserted
    List<Task> followupTasks = new List<Task>(); // build list in memory
    for (Opportunity opp : System.Trigger.new) {
        Task task = new Task(
          //WhoId = opp.Assigned_To__c, 
          //Description = opp.Synopsis__C, 
          Priority = 'High', 
          ReminderDateTime = opp.Due_Date__C, 
          Status = 'Not Started', 
          Subject = opp.Quick_Task_Subject__c);
        followupTasks.add(task);   // add to list
    }

        // insert the entire list
    insert followupTasks;  // NOTE: this is outside the above loop, only one insert is needed

 } // end of isAfter

}// end of trigger
Best Answer chosen by Admin (Salesforce Developers) 
Sebastian75Sebastian75

Hi,

 

Just had a quick look to your code and  I think you forgot to set the Task.WhatId field:

Task task = new Task(
//WhoId = opp.Assigned_To__c,
//Description = opp.Synopsis__C,
WhatID = opp.Id;
Priority = 'High',
ReminderDateTime = opp.Due_Date__C,
Status = 'Not Started',
Subject = opp.Quick_Task_Subject__c);

 Anyway, I'm not sure why you're doing this via a trigger rather than creating this follow up task via the Workflo.

 

Hope it helps...

 

 

 

All Answers

Sebastian75Sebastian75

Hi,

 

Just had a quick look to your code and  I think you forgot to set the Task.WhatId field:

Task task = new Task(
//WhoId = opp.Assigned_To__c,
//Description = opp.Synopsis__C,
WhatID = opp.Id;
Priority = 'High',
ReminderDateTime = opp.Due_Date__C,
Status = 'Not Started',
Subject = opp.Quick_Task_Subject__c);

 Anyway, I'm not sure why you're doing this via a trigger rather than creating this follow up task via the Workflo.

 

Hope it helps...

 

 

 

This was selected as the best answer
tedevangtedevang
Thanks - that was it. Basically my client wants to reduce the extra screen to enter an initial task for an opportunity so i just create it as part of the opportunity as a "Quick Task".
tedevangtedevang

Actually I have a followup question and issue. Below is the current trigger which works as it is written. Here is what I need:

 

  •  Upon saving the new task, I would like to clear the fields that were used to set up the task (since they will now appear in the task area)
  • The ActivityDate field gives me an error trying to set the value from Due_Date__C. Is there something I'm missing?

trigger taskCreator on Opportunity (before insert, before update, after insert) {
  if (Trigger.isBefore) { 
    Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>();

 } else if (Trigger.isAfter)  { 
    // after opportunities are inserted
    List<Task> followupTasks = new List<Task>(); // build list in memory
    for (Opportunity opp : System.Trigger.new) {
        Task task = new Task(
          WhatID = opp.Id,
          Priority = 'High', 
          //ActivityDate = opp.Due_Date__C, 
          Status = 'Not Started', 
          Subject = opp.Quick_Task_Subject__c);          
        followupTasks.add(task);   // add to list
    }

        // insert the entire list
    insert followupTasks;  // NOTE: this is outside the above loop, only one insert is needed

 } // end of isAfter

}// end of trigger
tedevangtedevang

OK - I had an uppercase "C" in Due_Date__c so that's fixed. Now I would like to just reset all the Opportunity custom fields related to Quick Task. How would I do that?

 

Thanks.

tedevangtedevang

Spoke too soon.

 

On ActivityDate = opp.Due_Date__c

 

I get this error: Invalid initial expression type for field Task.ActivityDate, expecting: Date at line 12 column 26. Do I need to specify somewhere that this is a date/time field?

rburnsrburns

Hello,

 

I've just stumbled across this post and is something that I would love to employ.

 

Can you tell if you managed to perfect this??

 

Richard.