+ Start a Discussion
MitchelSellersMitchelSellers 

Apex Trigger to Create Task from Lead?

I'm looking to have a trigger that when a lead is created, from a specific web-to-lead form that it will automatically create an activity.  But I'm not sure how to get started and can't seem to find any documentation on how I would do that.

 

I know how to create the trigger on the Leads, and have this for the stub.

 

trigger CreateActivity on Lead (after insert) {

}

Best Answer chosen by Admin (Salesforce Developers) 
BrianWKBrianWK

Martin,

 

Great question. We don't use Web-to-lead here, but I've played around with the feature some.

 

Unfortunately - I haven't found anything that's absolute positive.

 

But, there's the default Lead Owner that is setup in the "lead settings." Assuming you're using this user for your Web-to-Leads only, you could write a trigger based on the lead owner.

 

You may run into difficulties, If you're using assignment rules. I'm not sure what would be "run" first -- the before insert trigger or the assignment rule.

 

Another option is to use the Lead Source or other custom fields -- but that's less trustworthy since you have to depend on the end user filling it out.

 

So you could add a field that states "from web" and have this updated with the before insert trigger. That way you'll always know which came from Web-to-Lead.

 

Hope this helps! Good luck. Let us know what you do to get it to work.

All Answers

BrianWKBrianWK

Have you thought about using a workflow rule to create the task instead of using an Apex Trigger?

 

It may be more simple depending on your needs.

 

Else what you need to do is to have a Trigger on the lead that's After insert. within the trigger create a new task and insert that task. You'll need to have a list of the tasks that would be created, and a for loop that loops through all Leads that were created. Within that lead you create the task, add it to the list -- and at the very end insert all those new tasks. This will "bulk-ify" your code and support multiple leads being created at once.

 

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 = 'My new task',
       WhoID = trigger.new[i].id
       *Other Task Fields Here*
     );
   }
   insert lNewTasks;
}

  I would also recommend going to the resources page on developer.force.com and looking for the Developer's Apex Code developer's guide. There's also a cookbook, worksheets and lots of other material to help you get started.

 

Good luck!

Martin_DeloitteMartin_Deloitte

Hi BrianWK,

 

Do you know if there a way, in the trigger, to make a distinction between lead created from Web-to-lead form or lead created from Salesforce UI?

 

Thanks a lot.

 

BrianWKBrianWK

Martin,

 

Great question. We don't use Web-to-lead here, but I've played around with the feature some.

 

Unfortunately - I haven't found anything that's absolute positive.

 

But, there's the default Lead Owner that is setup in the "lead settings." Assuming you're using this user for your Web-to-Leads only, you could write a trigger based on the lead owner.

 

You may run into difficulties, If you're using assignment rules. I'm not sure what would be "run" first -- the before insert trigger or the assignment rule.

 

Another option is to use the Lead Source or other custom fields -- but that's less trustworthy since you have to depend on the end user filling it out.

 

So you could add a field that states "from web" and have this updated with the before insert trigger. That way you'll always know which came from Web-to-Lead.

 

Hope this helps! Good luck. Let us know what you do to get it to work.

This was selected as the best answer
Martin_DeloitteMartin_Deloitte

Thanks for your input!

 

We are using the lead source as a hidden field in our Web-to-lead form to verify that it is coming from the form.

Planning to use record types in the UI only to ensure we don't use the same lead source than the one used in the Web form.

 

Regards,