1

I am currently trying to implement a default action for the E-mail button on my Case Feed.

I am using the QuickAction.QuickActionDefaultsHandler to set my default actions, using the little bit of documentation I found here.

I am having two issues though and I can't seem to find anything to help me accomplish them.

1. Set The Default From E-mail Address

I would like the Case Feed to default to a pre-determined e-mail address, specified by the client.

2. Using An E-mail Template, Have Merge Fields Work Correctly

Currently, I am able to get my specified default template to show up in my Case Feed, however my merge fields are not being populated!

Here is most of the code that I have so far:

public with sharing class DefaultCaseFeedEmailImplementor implements QuickAction.QuickActionDefaultsHandler
{
    //This value will be filled in by a custom label
    private static final String DefaultEmailTemplateName = 'Case_Example';
    private static final Id DefaultEmailTemplateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = :DefaultEmailTemplateName LIMIT 1].Id;
    //This value will also be filled by a custom label
    private static final String EmailAddressOfInterest = '[email protected]';

    public void onInitDefaults(List<QuickAction.QuickActionDefaults> defaultsSettings) 
    {
        QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = GetSendEmailQuickActionFromDefaultSettings(defaultsSettings);
        if(sendEmailDefaults == null) return;

        EmailMessage emailMessage = (EmailMessage)sendEmailDefaults.getTargetSObject();

        //Trying to set a default from address for the e-mail from
        //Organization-Wide E-mail addresses
        List<OrgWideEmailAddress> organizationWideEmailAddresses = [SELECT Id FROM OrgWideEmailAddress WHERE Address = :EmailAddressOfInterest];

        if(organizationWideEmailAddresses.size() > 0)
            emailMessage.ReplyToEmailMessageId = organizationWideEmailAddresses[0].Id;

        sendEmailDefaults.setTemplateId(DefaultEmailTemplateId);
        sendEmailDefaults.setInsertTemplateBody(false);
        sendEmailDefaults.setIgnoreTemplateSubject(false);
    }

    private QuickAction.SendEmailQuickActionDefaults GetSendEmailQuickActionFromDefaultSettings(List<QuickAction.QuickActionDefaults> defaultsSettings) 
    {   
        for(QuickAction.QuickActionDefaults defaultSetting : defaultsSettings)
        {
            if(!(defaultSetting instanceof QuickAction.SendEmailQuickActionDefaults)) continue;

            if(QuickActionIsSendEmailQuickAction((QuickAction.SendEmailQuickActionDefaults)defaultSetting))
                return (QuickAction.SendEmailQuickActionDefaults)defaultSetting;
        }

        return null;
    }

    private Boolean QuickActionIsSendEmailQuickAction(QuickAction.SendEmailQuickActionDefaults actionToValidate)
    {
        return actionToValidate.getTargetSObject().getSObjectType() == EmailMessage.sObjectType &&
            actionToValidate.getActionName().equals('Case.Email') &&
            actionToValidate.getActionType().equals('Email');
    }
}

1 Answer 1

0

It turns out, I was on the right track.

Here is the code that I used to resolve the issue:

public with sharing class DefaultCaseFeedEmailImplementor implements QuickAction.QuickActionDefaultsHandler
{
    private static final String DefaultEmailTemplateName = Label.CaseFeedDefaultTemplate;
    private static final Id DefaultEmailTemplateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = :DefaultEmailTemplateName LIMIT 1].Id;
    private static final String DefaultFromAddress = Label.CaseFeedDefaultFromAddress;

    public void onInitDefaults(List<QuickAction.QuickActionDefaults> defaultsSettings) 
    {
        QuickAction.SendEmailQuickActionDefaults sendEmailDefaults = GetSendEmailQuickActionFromDefaultSettings(defaultsSettings);
        if(sendEmailDefaults == null && !Test.isRunningTest()) return;

        EmailMessage emailMessage = Test.isRunningTest() ? new EmailMessage() : (EmailMessage)sendEmailDefaults.getTargetSObject();  
        emailMessage.FromAddress = DefaultFromAddress;

        if(sendEmailDefaults != null) sendEmailDefaults.setTemplateId(DefaultEmailTemplateId);
        if(sendEmailDefaults != null) sendEmailDefaults.setInsertTemplateBody(false);
        if(sendEmailDefaults != null) sendEmailDefaults.setIgnoreTemplateSubject(false);

        if(Test.isRunningTest()) QuickActionIsSendEmailQuickAction(null);
    }

    private QuickAction.SendEmailQuickActionDefaults GetSendEmailQuickActionFromDefaultSettings(List<QuickAction.QuickActionDefaults> defaultsSettings) 
    {   
        for(QuickAction.QuickActionDefaults defaultSetting : defaultsSettings)
        {
            if(!(defaultSetting instanceof QuickAction.SendEmailQuickActionDefaults)) continue;

            if(QuickActionIsSendEmailQuickAction((QuickAction.SendEmailQuickActionDefaults)defaultSetting))
                return (QuickAction.SendEmailQuickActionDefaults)defaultSetting;
        }

        return null;
    }

    //I had to write this as one line......
    private Boolean QuickActionIsSendEmailQuickAction(QuickAction.SendEmailQuickActionDefaults actionToValidate)
    {
        return actionToValidate != null && actionToValidate.getTargetSObject().getSObjectType() == EmailMessage.sObjectType && actionToValidate.getActionName().equals('Case.Email') && actionToValidate.getActionType().equals('Email');
    }
}

It turns out, my merge fields were working properly, but the records I was working against didn't have the proper fields filled in.

You must log in to answer this question.

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