Skip to main content
  1. Salesforce.com/

3 Ways to Programmatically Create Tasks in Salesforce

·3 mins

When it comes to getting Salesforce to do what your business wants - there are options and more options. In other words, there are multiple ways to skin a cat - but do you know the best way?

Let me illustrate with a really simple example - what should be a straight-forward example of salesforce OOB vs. configuration vs. coding decisions.

Business problem #

Follow-up with the customer when an opportunity is lost. I will keep the details in the subsequent sections and not spoil the fun.

Option 1: Use Workflow #

Create a Workflow Rule that will create a task when pre-defined conditions are met.

Navigate to (Build) | Create | Workflow & Approvals | Workflow Rules in Salesforce Setup screen.

Create a new workflow rule to create a task when Opportunity status is ‘Closed Lost’.

workflow to create tasks.png

Select the option to create a new task when conditions are met. In the next screen you can specify values for the standard Task fields.

workflow for creating tasks in sfdc.png

You activate this workflow and sure enough - salesforce starts creating tasks and happily assigning those to the concerned person.

Advantages of this method -

  • Quick & easy configuration

Limitations -

  • Tasks will have all field values hard-coded in the template except for ‘Due Date’ (of course there are ways to update later - not in scope of this discussion!)

Option 2: Use Process Builder #

Process Builder was introduced this year - but I sure am impressed with what it can do, and if you ask me, that is the future of automation/validation in salesforce.

Navigate to (Build) | Create | Workflow & Approvals | Process Builder in Setup screen.

Click New to create a new Process - creating a trigger and an action is pretty easy. Just choose the object, fill in the criteria that needs to be evaluated and set-up the action for creating the Task.

salesforce process builder to create tasks.png

This will create the task similar to the previous approach but you have more control on what gets created.

Below is the UI that allows you to configure the “Immediate Actions” | “Create New Record” option or .

using quick actions to create new record in sfdc.png

You can refer dynamic field values, add more fields that need to be updated, and build in more powerful automation here.

Advantages -

  • Have dynamic field-values
  • Powerful!
  • Does not need coding expertise
  • Does not need unit testing etc. that is required for Apex

Limitations -

  • TBD!

Option 3: Use Apex #

If nothing else works, you always have Apex.

Here’s a simple way of creating a new task..

trigger Lost_Opportunity_Follow_up on Opportunity (after insert, after update) {
	Task[] oTasks = new List<Task>();
    for (Opportunity oOpty : trigger.new ) {
        if (oOpty.StageName == 'Closed Lost'){
            oTasks.add(new Task(Subject='Lost Opportunity Follow up AP', ActivityDate=Date.today()+10, WhatId=oOpty.Id,OwnerId=oOpty.OwnerId));
        }
    }
    if (oTasks.size()>0) insert oTasks;

}

Of course, it goes without saying that this is the last option that you need to use. There can be hidden complexities and further ways of optimization if you consider complex scenarios. This includes -

  • Control sequence of multiple triggers
  • Account for recursion
  • Future calls to improve performance and stay within limits

For a complete breakdown of what Apex coding can entail, head over to a post that describes how complexity can increase while using Apex .

Advantages -

  • Maximized flexibility - control when the task will be created, you can do batch processing instead of impacting performance
  • Really powerful!

Limitations -

  • Requires coding expertise
  • Need to write unit tests and ensure code coverage