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 = 'ff@gmail.com';
            emailMessage.FromAddress = 'tt@gmail.com';

            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 'support_technical@mycompany.com, tt@gmail.com'; 
    }

    private String getFromAddress(String reason){
        return 'loan@mycompany.com, personalloan@gmail.com'; 
    }
}

Please help, Hope nothing wrong in my code.

emailMessage.ValidatedFromAddress = 'tt@gmail.com';

'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

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 abc@abc.com or xyz@xyz.com. 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 = 'vignesh@orbitinnovations.com'; 
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('ab@ab.com'); 
}}}
0

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

emailMessage.FromAddress = getFromAddress(c.Reason);
emailMessage.FromAddress = 'tt@gmail.com';

I believe this should be enough and working:

emailMessage.fromAddress = ('tt@gmail.com');

Also, the email address 'tt@gmail.com' 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 '17 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.