Setting a default 'From' address when sending an email from Cases

global class EmailPublisherLoader implements QuickAction.QuickActionDefaultsHandler {  
   // Empty constructor  
   global EmailPublisherLoader() {  
   }  
   public string customLabelValue{get;set;}  
   // 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) {  
       Case c = [SELECT CaseNumber FROM Case   
            WHERE Id=:sendEmailDefaults.getContextId()];  
       EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();    
       // Set From address to make sure each email goes from sales@inmobi.com  
       emailMessage.FromAddress = getfromAddress(c.CaseNumber);  
   }  
  }  
 // return email address, Email Address is a from Address and this value is stored in custom label Email Address Label  
 private String getfromAddress(String reason) {  
    customLabelValue = System.Label.Email_Address_Label ;  
                 return customLabelValue;   
     }    
 }  
After creating Apex class , Go to case support settings  -> choose the Class Name -> Save 

Comments

  1. Hi,

    I get an error at this line:

    System.Label.Email_Address_Label

    Can you explain this please?

    Thank you, Rudolf

    ReplyDelete
    Replies
    1. Rudolf, Did you create a custom label for the class to reference? Setup>Create>Custom Labels

      Delete
  2. Hi Rudolf,

    You need to create a custom label as "Email Address Label" which will store the actual value of email address, the purpose to use custom label is that if in future you want to change the default email address then you just need to change the value in the label.

    Thanks,

    ReplyDelete
    Replies
    1. It doesn't work. I added the class with the custom label. Where exactly should we put the class under the Support Settings?. Are your sure is there where we have to put the class?

      Delete
    2. Hi,
      As suggested I have added ApexClass, Label and have setup the Apex Class in the support setting of the case, but it still doesn't work.

      When i click on the 'Send an Email' button from the Email related list of the case, it defaults the email address with the logged in user.

      Can you please advise.

      Thanks,
      Sapan

      Delete
  3. Hi,

    I used this code but FromAddress is not populating correctly. It doesn't take the value I provided instead the default value of the Contact's email. I tried setting BccAddress, and that is populating correctly.

    Can you help with this?
    Thanks

    ReplyDelete

Post a Comment

Popular posts from this blog

Test-Driven Development in Salesforce

Email Messaging from Salesforce