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
Lisa HorneLisa Horne 

Apex Trigger to Create Task from Lead

I need some assitance with creating a Trigger that creates a task from a lead record that has text in the Lead Web Comment Field to create a task for the Lead owner, with priority of Normal, Subject of Text "Lead Web Comment", Status of Lead Web Comment.

I have this trigger started but don't know exactly how to have all the fields populated with the above requirements.

Could someone please help?


trigger Lead_After_Insert on Lead (after insert)

{
  list<Task> lNewTasks = new list<Task>();

  for(integer i=0; i<trigger.new.size(); i++)

  {

     lNewTasks.add(MyTask = new Task(

       Subject = 'Lead Web Comment',

       WhoID = trigger.new[i].id

       *Other Task Fields Here*

     );

   }

   insert lNewTasks;

}
Best Answer chosen by Lisa Horne
mrtelesmrteles
Follow the trigger and test class
trigger CreateTaskOnLead on Lead (after insert) {
    List<Task> lTask = new List<Task>();
      Task t;
      if(Trigger.isAfter) {
        if(Trigger.isInsert) {
           for(Lead l: Trigger.new) {
               //Suggest to create a new value to Lead Source 
               //Picklist Like as Web-to-Lead and map your Web-to-lead to set Lead Source with this value 
               if((l.LeadSource != null)&&(l.LeadSource == 'Web-to-lead')){
                 t = new Task(); 
                 t.OwnerId = l.OwnerId;
                 t.Subject = 'Lead Web Comment';
                 t.Priority = 'Normal';
                 t.Status = 'Not Started';
                 t.Description = l.Description;
                 lTask.add(t);   
               }
            }
            if(!lTask.IsEmpty())
          		insert t;
           }        
      }
}
...and Test Class

@isTest
private class TestCreateTaskOnInsertWebToLead{
    @isTest static void TestCreateTaskOnInsertWebToLead(){
        Lead l = new Lead();
        l.LastName = 'Teles';
        l.Company = 'Comapany Teles';
        l.Status = 'Open - Not Contacted';
        l.LeadSource = 'Web-to-lead';
        
        insert l;
        
        Task t = new Task(); 
        t.OwnerId = '005o0000000UsQX';//Change 005o0000000UsQX to your UserId
        t.Subject = 'Lead Web Comment';
        t.Priority = 'Normal';
        t.Status = 'Not Started';
        t.Description = l.Description;
        
        insert t;
        
        
    }

}

This is the Test Class. Done. Now you are prepared to deploy and enjoy!

Please run all tests and Please let me know if I helped.

All Answers

Ramu_SFDCRamu_SFDC
You need to fill the below fields. Everything else in the trigger seems good

Task t = new Task();
   WhoId = trigger.new[i].Id;
   Subject = 'Web Lead';
   OwnerId = trigger.new[i].OwnerId
   ActivityDate = date.today();
Lisa HorneLisa Horne
Thanks,  Would you mind helping me make a Class for this?
Lisa HorneLisa Horne
Also what do I need to put for the Comment field in the activity to equal value in Web Lead Comment field?
mrtelesmrteles

Hi Lisa,

You don't need trigger to solve this problem. Just create a workflow like as ScreenShot below:

Workflow rule:
 
Workflow rule

Task associated with your workflow rule:

Task workflow


Please let me know if I helped.

 

Lisa HorneLisa Horne
Thanks, mrteles...but I need the comments in the activity record to equal the value of the text in the Web Lead Comments field.  Could a workflow do that?
mrtelesmrteles
No, in this case need trigger.

Give me a minutes to help you with the sample code.
mrtelesmrteles
Hi Lisa let's rock: 
trigger CreateTaskOnLead on Lead (after insert, after update) {
    List<Task> lTask = new List<Task>();
      Task t;
      if(Trigger.isAfter) {
        if(Trigger.isUpdate||Trigger.isInsert) {
           for(Lead l: Trigger.new) {
               //Suggest to create a new value to Lead Source 
               //Picklist Like as Web-to-Lead and map your Web-to-lead to set Lead Source with this value 
               if((l.LeadSource != null)&&(l.LeadSource == 'Web-to-lead')){
                 t = new Task();
                 //Change '005o0000000UsQX' with 
                 //your owner id or the queue....  
                 t.OwnerId = '005o0000000UsQX';
                 t.Subject = 'Lead Web Comment';
                 t.Priority = 'Normal';
                 t.Status = 'Not Started';
                 t.Description = l.Description;
                 lTask.add(t);   
               }
            }
            if(!lTask.IsEmpty())
          		insert t;
           }        
      }
}
Please let me know if I helped. 
Lisa HorneLisa Horne
Thanks Gabriel,  Question... is the Owner ID that you have equal to whoever the lead owner is?  Also,  I only need this trigger to run if there is text in the Web Lead Comment field on the lead record and then put that value from the Web Lead Comments field to be put in Activity comments field.  Is that what it will do?
mrtelesmrteles
Sorry Lisa, change the trigger to this:

trigger CreateTaskOnLead on Lead (after insert, after update) {
    List<Task> lTask = new List<Task>();
      Task t;
      if(Trigger.isAfter) {
        if(Trigger.isUpdate||Trigger.isInsert) {
           for(Lead l: Trigger.new) {
               //Suggest to create a new value to Lead Source
               //Picklist Like as Web-to-Lead and map your Web-to-lead to set Lead Source with this value
               if((l.LeadSource != null)&&(l.LeadSource == 'Web-to-lead')){
                 t = new Task();
                 t.OwnerId = l.OwnerId;
                 t.Subject = 'Lead Web Comment';
                 t.Priority = 'Normal';
                 t.Status = 'Not Started';
                 t.Description = l.Description;
                 lTask.add(t);  
               }
            }
            if(!lTask.IsEmpty())
            insert t;
           }       
      }
}


  • "the Owner ID that you have equal to whoever the lead owner is?"
    • The answer is.. depende, because maybe you need assigned the task for example to a queue of consultants, but it's possible. =)
  • "I only need this trigger to run if there is text in the Web Lead Comment field on the lead record and then put that value from the Web Lead Comments field to be put in Activity comments field.  Is that what it will do?
    • Yes, he will do it. I have 2 questions to u: "Your "Web-to-lead" are working correct ? When I put my information on the website... the field comments is mapping to field description Object Lead on Salesforce?"  if the answers is Yes. Done. This implementation solve your problem.
mrtelesmrteles
Follow the trigger and test class
trigger CreateTaskOnLead on Lead (after insert) {
    List<Task> lTask = new List<Task>();
      Task t;
      if(Trigger.isAfter) {
        if(Trigger.isInsert) {
           for(Lead l: Trigger.new) {
               //Suggest to create a new value to Lead Source 
               //Picklist Like as Web-to-Lead and map your Web-to-lead to set Lead Source with this value 
               if((l.LeadSource != null)&&(l.LeadSource == 'Web-to-lead')){
                 t = new Task(); 
                 t.OwnerId = l.OwnerId;
                 t.Subject = 'Lead Web Comment';
                 t.Priority = 'Normal';
                 t.Status = 'Not Started';
                 t.Description = l.Description;
                 lTask.add(t);   
               }
            }
            if(!lTask.IsEmpty())
          		insert t;
           }        
      }
}
...and Test Class

@isTest
private class TestCreateTaskOnInsertWebToLead{
    @isTest static void TestCreateTaskOnInsertWebToLead(){
        Lead l = new Lead();
        l.LastName = 'Teles';
        l.Company = 'Comapany Teles';
        l.Status = 'Open - Not Contacted';
        l.LeadSource = 'Web-to-lead';
        
        insert l;
        
        Task t = new Task(); 
        t.OwnerId = '005o0000000UsQX';//Change 005o0000000UsQX to your UserId
        t.Subject = 'Lead Web Comment';
        t.Priority = 'Normal';
        t.Status = 'Not Started';
        t.Description = l.Description;
        
        insert t;
        
        
    }

}

This is the Test Class. Done. Now you are prepared to deploy and enjoy!

Please run all tests and Please let me know if I helped.
This was selected as the best answer
Lisa HorneLisa Horne
Thanks mrteles,  What I need for the Owner of the task is for it to be assigned to whoever the owner is on the lead.  This could be different depending on who the Assignment rule assigns the lead to.
mrtelesmrteles
Hi Lisa,

Do you solved this question? If my solution works to you please mark her as a best answer.
Debra YoffieDebra Yoffie
Hi mrteles,
 
Thank you for the Workflow instructions. Is there a way to modify the Assigned To field in this Workflow so that is is a Pick List of Users, e.g. sales personnel, instead of having to put one person's name?
Laura WalkerLaura Walker
Hi
I have a similar query
I have a web to lead form and need to update a field on the lead created once the customer has paid.
We are putting a credit card payment module called Stripe on the return URL page
Any ideas how I can don this?
I realise that the leads are not created immediately but hopefully by the time that the payment has gone through it should be there?

Any assistance woudl be gratefully received
I am doing this work in my spare time for a charity and although experienced I am not a developer

Many thanks
Laura