Tuesday, 15 September 2020

[SOLVED]: Issue on getting Parent record-Id (whoid) while retrieving Task details generated through Email-To-Task

Requirement:
There was a requirement to track Sales person interaction with Customer which was being done via various Email Platforms such as Outlook, Gmail, Yahoo. We have used Email-To-Task to create task for such interactions. We need to track last interaction with customer by Sales\Support Person and get Due Date of Completed Task and auto populate it in "Last Sales Activity Date" (custom field) of related Contact/Lead record.

Challenge: 
The major challenge – When the Email is received to Salesforce, the task is generated and automatically linked to the relevant Contact/Lead record using Salesforce Email Configuration OOTB feature. For such Tasks, if Apex trigger is fired on the task object and at that time, we were unable to obtain the parent Id of Task generated via Email.

Solution:
To overcome this limitation, we developed a custom solution that involves creating an apex class named EmailTask having future method. Using the future method we can get parent recordId(Contact/Lead record), as it runs after some minor delay. The class contains the mechanism for retrieving Tasks List through Set of IDs that we need to pass as parameter from the Insert/Update Trigger. And after that, we need to get the Due Date (Activity Date) value from the Task and update to Last Sales Activity Date to the relevant parent record.

EmailTask Class


global class EmailTask {
    @future
    public static void getUpdate(set<Id> taskIds){
        //store WhoId and Task Record to the Map 
        Map<Id,Task> taskWhoMap = new map<Id,Task>();
        List<Task> TaskList = [Select Id,whoId,ActivityDate from task where Id IN :taskIds AND WhoId!= null];
        if(TaskList.size()>0){
            for(Task t : TaskList){
                taskWhoMap.put(t.WhoId,t); 
            }
        }
        
        if(taskwhomap.keySet().size()>0){
            List<Contact> contactList =[select Id, Last_Sales_Activity_Date__c from Contact where 
                                        Id =: taskwhomap.keySet()];
            
            if(contactList.size()>0){
                for(Contact c : contactList)
                {
                    if(taskwhomap.containsKey(c.Id)){
                        // store due date of task in Contact date field
                        c.Last_Sales_Activity_Date__c= taskwhomap.get(c.Id).ActivityDate;
                    }
                }
                // updating contact
                update contactList; 
            }
        }
        
        List<Lead> leadList =[select Id, Last_Sales_Activity_Date__c from Lead where 
Id =: taskwhomap.keySet()]; if(leadList.size()>0){ for(Lead l : leadList) { if(taskwhomap.containsKey(l.Id)){ // store due date of task in Lead date field l.Last_Sales_Activity_Date__c= taskwhomap.get(l.Id).ActivityDate; } } // updating Lead update leadList; } } }

Now, on the Task object, we need to configure an Apex Trigger which will initialize the EmailTask apex class on task creation/update.  

Email Trigger

trigger Email on Task (After insert,After update) {   
    if(trigger.Isafter){
        if(trigger.Isinsert || trigger.Isupdate){
            set<Id> TaskId = new set<Id>(); 
            for(Task t : trigger.new){
                TaskId.add(t.Id);
            }
            EmailTask.getUpdate(taskIds);
        }
    } 
}

Conclusion:

Using the above solution, we can achieve the requirement to generate the task for the relevant parent record, whenever the sales/support person communicates with the customer via E-mail and stores the interaction between them through generating Tasks and update the Last Sales Activity Date (custom field) to the relevant parent record. 

If you have any questions you can reach out our Salesforce Consulting team here.

No comments:

Post a Comment