• Vivek Suryawanshi 17
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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'; 
    }
}