Disable Task Assignment Email Notification in Lightning using @future

Share on facebook
Share on twitter
Share on linkedin
Salesforce allows the capability to disable the task assignment email notification when a task is assigned to any other user.

When a user creates a new Task and assigns it to himself, the Task assignment notification email will not be sent for that Task. However, if users create a Task and assign it to any other User, the Task’s assignee should receive the Task assignment email notification.

Salesforce allows the capability to disable the task assignment email notification when a task is assigned to any other user. This can be achieved by using EmailHeader.triggerUserEmail=false. When used with a VF page and Apex class, it works fine but if used with the Lightning component, it does not work.  

Below is the example using VF page:

TestTask.vfp

<apex:page controller="TestTaskCreate">
    <apex:form >
    <apex:commandButton action="{!createTasktest}" value="Create Task" id="theButton"/>
        </apex:form>
</apex:page>

TestTaskCreate.apxc

public class TestTaskCreate {
    public static void createTasktest()
    {
        createtask();
    }
    public static void createtask(){       
        Task newTask = new Task();
        newTask.WhoId = '003900000230qA4'; // contact Id
        newTask.Subject = 'Test Task email';
        newTask.ownerId = '0050I00000ARgwM'; //User Id should not be a logged in user
        
        Database.DMLOptions options = new Database.DMLOptions();
        options.EmailHeader.triggerUserEmail = false;
        Database.insert(newTask,options);
        
    }
}

This is a simple VF page with a button Create Task. By clicking this button, the Task assignment email notification should be sent to the user. As the apex class sets EmailHeader.triggerUserEmail=false, the Task assignment notification email will not be sent.

This is not the case when used in a Lightning component. Please find the example below:

TestTask.cmp

<aura:component controller="TestTaskCreate" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	<lightning:button variant="brand" name="Create Task" label="Create Task" onclick="{!c.taskCreate}" />
</aura:component>

TestTaskController.js

({
	taskCreate : function(component, event, helper) {
         var action = component.get('c.createTasktest');
        action.setCallback(this, function(response) {
            var stateValue = response.getState();
            if(stateValue === 'SUCCESS'){
               alert('task created');
            } });
        $A.enqueueAction(action);
	}
})

TestTaskCreate.apxc

public class TestTaskCreate {
    @AuraEnabled 
    public static void createTasktest()
    {
        createtask();
    }   
    public static void createtask(){        
        Task newTask = new Task();
        newTask.WhoId = '003900000230qA4';
        newTask.Subject = 'Test Task email';
        newTask.ownerId = '0050I00000ARgwM'; 
        
        Database.DMLOptions options = new Database.DMLOptions();
        options.EmailHeader.triggerUserEmail = false;
        Database.insert(newTask,options);
    }
}

This Lightning component has a button Create Task and uses the same apex class with @auraenabled method to create a task. Even though EmailHeader.triggerUserEmail = false has been set in the apex class, the Task assignment email notification is still sent to the user. The workaround is to execute the createtask() method in asynchronous mode by using @future

@future
Public static void createtask(){} 

When the createtask() method is called in async mode, the EmailHeader.triggerUserEmail = false  prevents the Task assignment email notification.

Reference

https://help.salesforce.com/articleView?id=000330196&type=1&mode=1

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.