Assign Tasks in Salesforce Using Apex

Usually, when we want to assign a task to users or a queue, we do it manually from the record detail page.

But what if I told you we can automate this task assignment process using a Salesforce Apex class?

Yes, with Apex, we can write logic to automatically assign tasks to specific users or queue members based on certain conditions, like when a record is created, updated, or meets a specific criteria.

In this Salesforce tutorial, we will learn how to assign tasks in Salesforce using Apex with step-by-step explanations.

What is a Task in Salesforce?

Salesforce tasks are like reminders for users to pay attention to a particular record. They help users keep track of tasks they need to do, such as following up with a customer, scheduling a meeting, or checking in on an issue.

Each task has a due date and a priority level (high, normal, or low) and is linked to a contact and a leads object record. We can also add notes to a task to keep track of any details you don’t want to forget.

Below, I have explained task fields and their uses:

Fields NameUses
SubjectHere we can specify the intention of the task (like “Call Client”).
Assigned ToHere we can see to whom we have assigned a task, whether it is a user or a queue.
WhatIdHere, we can specify the objects related to the current object, such as Account, Opportunity, etc.
StatusThe progress of the task (e.g., Not Started, Completed).
WhoId The contact or lead of the task is about.
PriorityHigh, Normal, or Low.
Create Task in Salesforce

What is the use of a task in Salesforce Apex?

In Salesforce, we have a flow automation feature that allows us to automate processes such as record updates, task assignments, creation of related records, and sending automated emails.

However, we can use Salesforce Apex when the logic is complex or when working with a large number of records. Triggers are written in code, which can automate the process of assigning tasks to users or queues.

Using Apex to assign tasks to users helps you automatically assign work to users in Salesforce. This saves time, avoids doing things manually, and ensures the right user is reminded to take action when something important happens.

To whom can we assign a Task in Salesforce?

In Salesforce, we can assign a task to the following users:

  • Users: We can assign a task to any active Salesforce User. These are individual users who have a user license and are logged into Salesforce.
  • Queues: We can also assign a task to a Queue, but only if the task is related to an object that the queue supports, like cases, leads, or Custom Objects enabled for queues.

We cannot assign tasks to groups or roles in Salesforce, but queues are also part of a group.

Queues are stored internally in an object called Group, which is why we use it to query queue information. The Group object holds different types of groupings, such as public groups, roles, and queues.

Assign Task to User Using Salesforce Apex

Below, we will see, using the example, how we can assign the task to the record owner (user) in Salesforce Apex.

For example, we want to automatically create and assign a follow-up task to the case owner every time a new high-priority case is created.

In the Apex trigger below, we created a list collection to store the tasks that will be created.

Then, in for each loop, we iterate over the new case records, for which we used the Trigger.new context variable.

Using the if condition, we checked that if the priority is ‘High,’ then only the task should be created and assigned to the record/case owner.

To create the task, we need to declare the task object instance variable and use that variable to access the task’s fields from Salesforce and assign values to those fields.

trigger CreateTaskOnCase on Case (after insert) {
    
   List<Task> taskList = new List<Task>(); 

    for (Case c : Trigger.new) {

        if (c.Priority == 'High') {

            Task t = new Task();

            t.Subject = 'Follow up with high-priority customer';
            t.Status = 'Not Started';
            t.Priority = 'High';
            t.OwnerId = c.OwnerId; 
            t.WhatId = c.Id;       
            t.ActivityDate = Date.today().addDays(2);

            taskList.add(t);

        } else {
            System.debug('priority is not High');
        }
    }

    if (!taskList.isEmpty()) {
        insert taskList;
    }
}

After that, navigate to the case object and create a high-priority case to check whether it will get assigned to the case owner.

So here I created the case and saved it.

Assign Task to User Using Salesforce Apex

After that, navigate to the task object. There, you will see that the task is assigned to the case owner with the values that we provided to the task fields in the Apex class.

Assign Tasks in Salesforce Using Apex

In this way, we can automatically assign a task to a specific user using the Apex class in Salesforce when specific conditions are met.

Assign Task to Queue Using Salesforce Apex

Now, let’s understand how to assign tasks to a queue using Apex in Salesforce.

For example, whenever a new lead is created with Lead Source = ‘Phone Inquiry’, a task should be created and assigned to a specific queue (e.g., Phone Inquiry Queue). The task will remind the team to call the lead within 1 day.

Before that, we should have a queue with permissions for both the object on which they will work and the task object, because they will also work on the task object.

In the image below, you can see I have created a queue named ‘Phone Inquiry Queue’ and given permission to lead and task objects.

Also, I have added the users in the queue so that anyone can work on that task from those users, and this is nothing but multiple users or a group of users working on the same task.

Assign Task to Queue in Salesforce

In the below Apex code, using the SOQL query, we first fetched the queue to which we want to assign the task. In Salesforce, queues are stored in the Group object, which is why we query from the Group object instead of something like Queue.

Then again we created a list collection to store the tasks that will be created, and in for each loop, we iterate over the new case records, for which we used the Trigger.new context variable.

Using the if condition, we checked that if the lead source is ‘Phone Inquiry,’ then only the task should be created and assigned to the specified queue.

To create the task, we need to declare the task object instance variable and use that variable to access the task’s fields from Salesforce and assign values to those fields.

trigger AssignTaskToQueue on Lead (after insert) {

Group taskQueue = [SELECT Id FROM Group WHERE Name = 'Phone Inquiry Queue' AND Type = 'Queue' LIMIT 1];

    List<Task> taskList = new List<Task>();

    for (Lead l : Trigger.new) {

        if (l.LeadSource = 'Phone Inquiry') { 

            Task t = new Task();

            t.Subject = 'Follow up this lead';
            t.Status = 'Not Started';
            t.Priority = 'High';
            t.OwnerId = taskQueue.Id; 
            t.WhoId = l.Id;             
            t.ActivityDate = Date.today().addDays(1); 

            taskList.add(t);
        }
    }

    if (!taskList.isEmpty()) {
        insert taskList;
    }
}

After that, navigate to the lead object and create the lead, where the lead source should be the ‘Phone Inquiry’ because only this lead record will create the task and assign it to the defined queue.

Assign Task to Queue Using Salesforce Apex

Now, again navigate to the task object, and there you will see the task is assigned to the queue and the task fields’ values populated as we provided in the Apex class.

How to Assign Task to Queue Using Salesforce Apex

In this way, we can create tasks for the objects and assign them to the users or queues in Salesforce using Apex.

Conclusion

I hope you have got an idea about how to assign tasks in Salesforce using Apex with step-by-step explanations. I have explained the task object, the use of the task object in Apex, and to whom we can assign tasks in Salesforce.

After that, using the example, I have explained step by step how we can assign tasks to users and queues in Salesforce using Apex.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.