function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andy StollmeyerAndy Stollmeyer 

Converting Trigger to @future due to SFDC BCC logic

Hi,

I need to convert the below Trigger into an @future, which I have no knowledge of.  I tried to simply add the annotation "@future" but apparently you can't pass sObjects to @future methods.  I am really stuck here, any help is greatly appreciated!

Trigger
//TASK TRIGGER
trigger TaskTrigger on Task (after insert) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            TaskTriggerHandler inserter = new TaskTriggerHandler();
            inserter.InsertContactUpdateStatus(Trigger.new);
        }
    }
}
Class:
public with sharing class TaskTriggerHandler {

    public void InsertContactUpdateStatus(List<Task> newList){
        set<Id> ctIds = new set<Id>();
        set<Id> callIds = new set<Id>();
        set<Id> emailIds = new set<Id>();
        for(Task t: newList){
            String whoId = t.WhoId;
            if(whoId!=null && whoId.startsWith('003')){
                ctIds.add(t.WhoId);
                if(t.isClosed == TRUE){
                    if(t.Type != null && t.Type == 'Call'){
                        callIds.add(t.WhoID);
                    }
                    if(t.Type != null && t.Type == 'Email'){
                        emailIds.add(t.WhoID);
                    }
                }
            }
        }
        BUNCH OF ADDITIONAL LOGIC THAT I DONT THINK IS IMPORTANT
    }
}

 
Best Answer chosen by Andy Stollmeyer
Mahesh DMahesh D
Hi Andy,

Please find the modified code here:

Here I considered:

(1) Better way of writing the code.
(2) Modified the if condition in the class.

 
trigger TaskTrigger on Task (after insert) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          //call @future trigger MD- Modified
            TaskTriggerHandler.InsertContactUpdateStatus(Trigger.newMap.keySet());
        }
    }
}
 
public with sharing class TaskTriggerHandler {

    @future
    public static void InsertContactUpdateStatus(Set<ID> tIDs){
        set<Id> ctIds = new set<Id>();
        set<Id> callIds = new set<Id>();
        set<Id> emailIds = new set<Id>();
        List<Task> newList = [SELECT ID, WhoID, Status, Type, IsClosed FROM Task WHERE ID in: tIDs]; 
        
        for(Task t: newList){
            // MD - Modified
            If(t.WhoId != null && t.WhoId.getsObjectType() == Contact.sObjectType) {            
                ctIds.add(t.WhoId);
                if(t.isClosed == TRUE){
                    if(t.Type != null && t.Type == 'Call'){
                        callIds.add(t.WhoID);
                    }
                    if(t.Type != null && t.Type == 'Email'){
                        emailIds.add(t.WhoID);
                    }
                }
            }
        }        
    }
}

Thai will help you not only for this issue, even in the future if you get a chance to work on somilar requirement then it is easy to handle it.

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Andy StollmeyerAndy Stollmeyer
Nm, this was much easier than I thought it would be.  You simply need to use a Set or List of IDs rather than an sObject list and then query your object based on that Set/List.

Trigger:
trigger TaskTrigger on Task (after insert) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          //call @future trigger
            Set<ID> taskIDs = new Set<ID>();
            for(Task t: Trigger.new){
                taskIDs.add(t.ID);
            }
            TaskTriggerHandler.InsertContactUpdateStatus(taskIDs);
        }
    }
}
Class:
@future
    public static void InsertContactUpdateStatus(Set<ID> tIDs){
        set<Id> ctIds = new set<Id>();
        set<Id> callIds = new set<Id>();
        set<Id> emailIds = new set<Id>();
        List<Task> newList = [SELECT ID, WhoID, Status, Type, IsClosed FROM Task WHERE ID in: tIDs]; 
        
        for(Task t: newList){
            String whoId = t.WhoId;
            if(whoId != null && whoId.startsWith('003')){
                ctIds.add(t.WhoId);
                if(t.isClosed == TRUE){
                    if(t.Type != null && t.Type == 'Call'){
                        callIds.add(t.WhoID);
                    }
                    if(t.Type != null && t.Type == 'Email'){
                        emailIds.add(t.WhoID);
                    }
                }
            }
        }
        BUNCH OF ADDITIONAL LOGIC THAT DOESNT MATTER
    }
}

 
Mahesh DMahesh D
Hi Andy,

Please find the modified code here:

Here I considered:

(1) Better way of writing the code.
(2) Modified the if condition in the class.

 
trigger TaskTrigger on Task (after insert) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
          //call @future trigger MD- Modified
            TaskTriggerHandler.InsertContactUpdateStatus(Trigger.newMap.keySet());
        }
    }
}
 
public with sharing class TaskTriggerHandler {

    @future
    public static void InsertContactUpdateStatus(Set<ID> tIDs){
        set<Id> ctIds = new set<Id>();
        set<Id> callIds = new set<Id>();
        set<Id> emailIds = new set<Id>();
        List<Task> newList = [SELECT ID, WhoID, Status, Type, IsClosed FROM Task WHERE ID in: tIDs]; 
        
        for(Task t: newList){
            // MD - Modified
            If(t.WhoId != null && t.WhoId.getsObjectType() == Contact.sObjectType) {            
                ctIds.add(t.WhoId);
                if(t.isClosed == TRUE){
                    if(t.Type != null && t.Type == 'Call'){
                        callIds.add(t.WhoID);
                    }
                    if(t.Type != null && t.Type == 'Email'){
                        emailIds.add(t.WhoID);
                    }
                }
            }
        }        
    }
}

Thai will help you not only for this issue, even in the future if you get a chance to work on somilar requirement then it is easy to handle it.

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Andy StollmeyerAndy Stollmeyer
Thanks, Manesh!  Appreciate the reply and additional best-practices.