+ Start a Discussion
NightFoxNightFox 

Pre-populate FROM address in Standard Email Publisher action using QuickAction.QuickActionDefaultsHandler

How to pre-populate FROM address in Email Publisher in the Case Feed. 
As far as I know My below code is correct. 

Here is the place where im passing the value for FROM Address fields. 
 
EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();    
// Set bcc address to make sure each email goes for audit
emailMessage.FromAddress = ('security@test.com');
emailMessage.ValidatedFromAddress = ('ValidatedFrom@address.com'); emailMessage.BccAddress = 'ff1@gmail.com';
emailMessage.CCAddress = 'bb1@gmail.com';
emailMessage.subject= 'Testing ';
emailMessage.ToAddress = 'toAdd@gmail.com';
Here is the Email publisher action page. See, all other fields are populated as per Apex definition. From address is only considering the ORG WIDE ADDRESS.

User-added image

Here is my full APEX code:
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(sendEmailDefaults != null){
            system.debug(' sendEmailDefaults # ' + sendEmailDefaults  );
            system.debug(' sendEmailDefaults ID # ' + sendEmailDefaults.getcontextId()); 
            Case c = [SELECT Status, Casenumber,Reason FROM Case WHERE Id=:sendEmailDefaults.getContextId()];
            system.debug(' Case '+ c.CaseNumber);
            system.debug(' sendEmailDefaults.getTargetSObject() '+ sendEmailDefaults.getTargetSObject());
        
            EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();    
            // Set bcc address to make sure each email goes for audit
            system.debug(' getBccAddress(c.Reason) # ' + getBccAddress(c.Reason) );
            system.debug(' getFromAddress(c.Reason) # ' + getFromAddress(c.Reason));
            //emailMessage.BccAddress = getBccAddress(c.Reason);
            //emailMessage.FromAddress = getFromAddress(c.Reason);
            emailMessage.FromAddress = ('security@test.com');
            emailMessage.ValidatedFromAddress = ('ValidatedFrom@address.com');
            emailMessage.BccAddress = 'ff1@gmail.com';
            emailMessage.CCAddress = 'bb1@gmail.com';
            //emailMessage.ValidatedFromAddress = 'tt1@gmail.com';
            emailMessage.subject= 'Testing ';
            emailMessage.ToAddress = 'toAdd@gmail.com';
            system.debug(' emailMessage '+ emailMessage );
            
            //sendEmailDefaults.ValidatedFromAddress = 'tt1@gmail.com';
            
            /* 
            Set Template related fields 
            When the In Reply To Id field is null we know the interface 
            is called on page load. Here we check if 
            there are any previous emails attached to the case and load 
            the 'New_Case_Created' or 'Automatic_Response' template.
            When the In Reply To Id field is not null we know that 
            the interface is called on click of reply/reply all 
            of an email and we load the 'Default_reply_template' template
            */
            
            system.debug(' sendEmailDefaults get Reply To '+ sendEmailDefaults.getInReplyToId());
            system.debug(' sendEmailDefaults getFrom Address '+ sendEmailDefaults.getFromAddressList());
            
            Integer emailCount = [SELECT count() FROM EmailMessage WHERE ParentId=:sendEmailDefaults.getContextId()];
            
            system.debug(' emailCount ' + emailcount );
            
            if(emailCount!= null && emailCount > 0){
                sendEmailDefaults.setTemplateId(
                getTemplateIdHelper('Send_Personal_Loan_Details'));
            }else{
                sendEmailDefaults.setTemplateId(
                getTemplateIdHelper('New_Case_Created'));
            }
            
            sendEmailDefaults.setInsertTemplateBody(false);
            sendEmailDefaults.setIgnoreTemplateSubject(true);
            
            system.debug(' sendEmailDefaults ### ' + sendEmailDefaults);
            
            /*
            if(sendEmailDefaults.getInReplyToId() == null){
                
            }else{
                sendEmailDefaults.setTemplateId(
                getTemplateIdHelper('Default_reply_template'));
                sendEmailDefaults.setInsertTemplateBody(false);
                sendEmailDefaults.setIgnoreTemplateSubject(true);
            }*/
        }
    }
    
    private Id getTemplateIdHelper(String templateApiName){
        Id templateId = null;
        try{
            templateId = [select id, name from EmailTemplate where developername = : templateApiName].id;
            system.debug(' Temp id # '+ templateId );   
        }catch(Exception e){
            system.debug('Unble to locate EmailTemplate using name: ' + templateApiName + ' refer to Setup | Communications Templates ' + templateApiName);
        }
        return templateId;
    }
    
    private String getBccAddress(String reason){
        return 'support_technical@mycompany.com, tt@gmail.com'; 
    }
    
    private String getFromAddress(String reason){
        return 'loan@mycompany.com, personalloan@gmail.com'; 
    }
}




 
Arun Prasath 5Arun Prasath 5
Add the Sender email address in Organisation Wide Default to pre-populate.
NagendraNagendra (Salesforce Developers) 
Hi,

Have you put this "from" address in your org wide email and give a try which should probably do the trick for you.

Please let us know if this helps.

Thanks,
Nagendra
NightFoxNightFox
Here is my requirement,

Custom Obj:  Group Name (eg group name : G1 and G2)
Child Obj : Group Member (A1, A2, A3 and A4)
G1 - abc@gmail.com, efg@gmail.com
G2 - xyz@gmail.com, ijk@gmail.com

Now consider,
Agent A1, A2, A3 present in group G1  
Agent A1, A2, A4 present in group G2 (A1 and A2 present in both group)

When a has been raised for G1 group, the A1 and A2 agents must see the "abc@gmail.com, efg@gmail.com" email address in case feed email publisher action From Address field

When a G2 group Agent A4 opens a new case, he/she must see the "xyz@gmail.com, ijk@gmail.com" email address in case feed email publisher action - From Address field.

In short, I can say that the "From Address" in case feed email publisher action has to be changed with respect to the Group Name for which the case has been raised.  

Screen shot of case feed email publisher action - Please note that the FROM ADDRESS field in the right top corner has to be changed dynamically.

Here is the Screen shot of Case Feed Email Publisher Action. 
User-added image
 
Vivek Suryawanshi 17Vivek Suryawanshi 17
I was facing same issue where From Address was not getting populated even after creating apex class which implements QuickAction.QuickActionDefaultsHandler, I was able to find solution by following below steps

1. Create apex class by referring https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_QuickAction_QuickActionDefaultsHandler.htm

2. Add below code to override To and From Address
           EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();    
            // Set BCC address to make sure each email goes for audit
            emailMessage.BccAddress = 'vivek.a.suryawanshi@test.com';
            emailMessage.ToAddress = 'test@test.com';
            emailMessage.FromName = 'VivekS';
            emailMessage.ValidatedFromAddress = 'vivek.a.suryawanshi@test.com';
            emailMessage.FromAddress = 'vivek.a.suryawanshi@test.com';

3. Make sure that emailid mentioned in From address is registered/Verified with Salesforce, when I say Verified it should be either verified by setting in Email-to-Case or if that mailbox is not confiured than verify that emailid on Organization-Wide Addresses

4. Update Support settings to call apex class by enabling setting named "Enable Default Email Templates or the Default Handler for Email Action"