1
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.BccAddress = '[email protected]';
            emailMessage.FromAddress = '[email protected]';

            system.debug(' emailMessage '+ emailMessage );

            /* 
            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(false);

            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 '[email protected], [email protected]'; 
    }

    private String getFromAddress(String reason){
        return '[email protected], [email protected]'; 
    }
}

Please help, Hope nothing wrong in my code.

emailMessage.ValidatedFromAddress = '[email protected]';

'ValidatedFromAddress' is a picklist field, which is the only field you could see in the email publisher action. Even tried to change that field as well. yet it doesn't make any difference. Kindly share your thoughts if you have done such kind.

But the From Addresses is not getting changed from default Email Id

3 Answers 3

1

With the below snippet, If I remove any email address from the FROM address dropdown, its working but if I try to add any new email Id its not working.

However, I feel its quite reasonable when salesforce doesn't allow us to add any new address because what if we add any FROM address which is not even exist in any domain like [email protected] or [email protected]. So, saleforce wants to show only registered and verified email Id in FROM address dropdown.

When we want to add any new address, we have to first register in "Organization-Wide Email Addresses", "Email-to-Case" or it should a SF user's email address.

We can resolve our requirement by removing the email address which is not relevant to the User through apex.

Snippet

emailMessage.FromAddress = '[email protected]'; 
LIST<String> fromList = sendEmailDefaults.getFromAddressList(); 
if(fromList != null){ 
for(Integer i = fromList.size() - 1; i >= 0; i--){ 
if(fromList.get(i).equals(emailMessage.FromAddress)){ 
fromList.remove(i);
fromList.add('[email protected]'); 
}}}
1
  • Hey! I understand this is a 5 years old answer, but this code seems to be working for others and not for me. Using this code, debug logs show the From Address List with the filtered addresses list, but the email composer is still showing all email addresses anyway. Any ideas why this is happening? Am I missing something here? Thanks in advance!
    – Marcel
    Jul 18, 2022 at 7:20
0

Not sure why you are calling 'emailMessage.FromAddress' twice:

emailMessage.FromAddress = getFromAddress(c.Reason);
emailMessage.FromAddress = '[email protected]';

I believe this should be enough and working:

emailMessage.fromAddress = ('[email protected]');

Also, the email address '[email protected]' should be a valid Email Address in your Email-To-Case settings, and also available in the related Page Layout (under 'Feed View' -> 'Specify From Address(es)').

1
  • Yeah, you are right, I know this already, yet my query is not resolved.
    – Saravana
    Jun 28, 2017 at 9:26
0

There are actually two From fields in the Send Email action:

  • From: Is a dropdown field that lets the user choose a From address from a defined list of addresses.
  • FromAddress: Is an email text field that just includes an email address.

I think you are setting the FromAddress field in your code, but are displaying the From field in the UI.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .