+ Start a Discussion
Jim Parker 7Jim Parker 7 

QuickAction.QuickActionDefaultsHandler

Trying to adapt the code from:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_QuickAction_QuickActionDefaultsHandler.htm#apex_interface_QuickAction_QuickActionDefaultsHandler_Example

Effectively, on the case quick email action, it automatically strips off CC'd email addresses when you hit reply.  So I am trying to pre-popualte them.  The issue I am having is I don't know how to lookup the email (and the associated to, CC and from addresses) that I am replying to on the case or the most recent email that is registered on the case.  Please help!
 
global class EmailPublisherLoader implements QuickAction.QuickActionDefaultsHandler {
    // Empty constructor
    global EmailPublisherLoader() {
    }
    
    // The main interface method
    global void onInitDefaults(QuickAction.QuickActionDefaults[] defaults) {
        QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = null;
    
    
        // Check if the quick action is the standard Case Feed send email action
        for (Integer j = 0; j < defaults.size(); j++) {
            if (defaults.get(j) instanceof QuickAction.SendEmailQuickActionDefaults && 
               defaults.get(j).getTargetSObject().getSObjectType() == 
                   EmailMessage.sObjectType && 
               defaults.get(j).getActionName().equals('Case.Email') && 
               defaults.get(j).getActionType().equals('Email')) {
                   sendEmailDefaults = 
                       (QuickAction.SendEmailQuickActionDefaults)defaults.get(j);
                   break;
            }
        }
        
        //If stardard case feed email action
        if (sendEmailDefaults != null) {
            
            //Find the case
            Case c = [SELECT Status, Reason, SuppliedEmail, ContactEmail FROM Case 
                      WHERE Id=:sendEmailDefaults.getContextId()];
        
            EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();    
			
            
            //No idea how to get the most recent email attached to the case......
            EmailMessage prevEmail = [Select ccAddresses, toAddresses, fromAddress from InboundEmail where ParentId=:sendEmailDefaults.getContextId()]
           
            //on Page load, not reply/reply all to a message
            if (sendEmailDefaults.getInReplyToId() == null) {
                
                //Count to see if previous emails on case
                Integer emailCount = [SELECT count() FROM EmailMessage 
                                      WHERE ParentId=:sendEmailDefaults.getContextId()];
                               
                //previous email attached to existing case
                if (emailCount!= null && emailCount > 0) {
            		              
                    // Set addresses
            		emailMessage.ccAddress = prevEmail.CcAddress;
                    emailMessage.toAddress = prevEmail.fromAddress;
                   
                } 
                
                //no previous email attached
                else {
                    emailMessage.ccAddress = '';
                    emailMessage.toAddress = '';
                    
                }
            } 
            
            //When user hits reply or reply all
            else {
                    // Set addresses
            		emailMessage.ccAddress = prevEmail.CcAddress;
                    emailMessage.toAddress = prevEmail.fromAddress;
                               
            }
        }
    }    
}