Reusable Activity Assignment to Queue by Process builder Invocable Action

If business user needs to assign a Task toa Queue and Users using process builder, use this reusable Apex invocable method with wrapper class feature.

Introduction: 

If business user needs to assign a Task to a Queue and Users using process builder,  use this reusable Apex invocable method with wrapper class feature. 

Why specially use this invocable method with wrapper class? 

Let us look at one scenario. Ibusiness user needs to assign a Task record to queue and users, as a developer, we prefer to use Process Builder. Process builder assigns task record to user easily but to assign to a  queue, the developer needs to do some custom work. 

Use Case: 

Scenario:
The business wants to assign task to queue or another user. Initially Setup the process builder with apex method invocable action.  

Step 1 : Create Process Builder with Apex Invocable action.  

Step 2: Create an Apex Invocable method with Wrapper class. In the wrapper class, declare the variables with Invocable annotation.

@InvocableVariable – It helps to make the variable as input and output for apex Invocable method. Depending on thebusiness use case, declare some variable related to task object, specially to assign task record to queue. Declare Assign To field as @Invocable Variable with required option.  

Apex Class:  

global without sharing class TaskInvokeHandler_AC { 

/* 

* This method is to create a task when an Process Builder Invoke this Method. 

**/ 

@InvocableMethod(Label=’TaskCreateHandler’) 

global static void createTask(List<TaskCreatedHandler> handlers){ 

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

    Set<String> queueNames = new Set<String>(); 

    Set<String> recordTypeNames = new Set<String>(); 

    Map<String,IdqueueMap = new Map<String,Id>(); 

    Map<String,IdrecordTypeMap = new map<String,Id>(); 

    for(TaskCreatedHandler handler:handlers){ 

        recordTypeNames.add(handler.recordTypeName); 

        if(!handler.assignedTo.startswith(‘005’)) 

            queueNames.add(handler.assignedTo); 

    }      

    for(Group queue: [SELECT Id,DeveloperName FROM Group WHERE Type=’QUEUE’ AND DeveloperName IN:queueNames]){ 

        queueMap.put(queue.DeveloperName,queue.Id); 

    }      

    for(RecordType record:[SELECT Id,Name FROM RecordType WHERE SobjectType=’Task’ AND Name IN:recordTypeNames]){ 

        recordTypeMap.put(record.Name,record.Id); 

    } 

    for(TaskCreatedHandler handler:handlers){ 

        Task task = new Task(); 

        task.whatId = handler.relatedToId; 

        task.Subject = handler.subject; 

        task.ActivityDate = handler.dueDate; 

        task.RecordTypeId = recordTypeMap.get(handler.recordTypeName); 

        task.OwnerId = !queueMap.isEmpty()?queueMap.get(handler.assignedTo):handler.assignedTo; 

        tasksToCreateLst.add(task);   

    } 

    if(tasksToCreateLst.size() > 0){ 

        try{ 

            insert tasksToCreateLst; 

        } 

        catch(Exception e){ 

            System.debug(‘Exception: ‘+e.getMessage()); 

        } 

    } 

}  
global class TaskCreatedHandler { 

        @InvocableVariable(required=true) 

        public Id relatedToId; 

        @InvocableVariable(required=true) 

        public String subject; 

        @InvocableVariable(required=true) 

        public string assignedTo; 

        @InvocableVariable(required=true) 

        public Date dueDate; 

        @InvocableVariable(required=true) 

        public String recordTypeName; 

    } 

} 

             Step 3: Refer this Invocable method on apex action type in Process Builder and assign the field values of task by providing input using wrapper method and its invocable variable.  

Step 4: List of input variables in the process builder. Here, assign corresponding queue name or user to assigned To variable. Besides owner assignment, we can assign the record type for task also.  

Step 5: Finally activate and testhe business scenarios  

References:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableVariable.htm

Consideration:
While verifying this functionality in Sandbox or production, we should be aware that the assigned queue must have enabled the Task object as supported object list.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.