function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
hy.lim1.3897974166558203E12hy.lim1.3897974166558203E12 

How can I default an email template when click 'Reply' or 'Reply All' on Case Feed item?

Hi, as title, how can I default an email template when click 'Reply' or 'Reply All' button on Case Feed item? Basically I would like to load a simple reply signature template when users click on  'Reply' or 'Reply All' button on Case Feed email feed item.

 I understand that the Smart Templates (Enable Default Email Templates) feature allows you to drive which template is pre-loaded from any apex logic you create, but that is for when you typing a new email. I would like to load a template when click on 'Reply' or 'Reply All' button and my template will be the top of the message and follow by my 'original message'

Is that possible?
Sonam_SFDCSonam_SFDC
Yes, you are right. The Enable Default Email Templates is for the Case feed and helps get a default template selected when composing a new email.

To get an email template when choosing to Reply or Reply All, you will have to write a custom code(mail functionality) as the email related list is not customizable at this time so you can't really choose a default template for the links in this list.
Kevin StrangeKevin Strange
As of Winter 15 - the Reply and Reply All buttons in the case feed divert directly to the email publisher above - rather than going into the "hardcoded, 8 year old, email sending template that creates a closed task activity and is 100% not customizable. This is a step in the right direction and I hate to say it but user training and this new change helps a little. You can also remove the Email related list (which is the only way to remove the "Send email" button) and add a visualforce page related list (buttonless) - this will work :-)
Ido Greenbaum 10Ido Greenbaum 10
Hi Soman_SFDC,
Can you please assist with pointing out the way to accomplish setting a Template on the 'Reply' and 'ReplyAll' with a Custom Code? I have the Default Template Switcher implemented and working allright, but suffer from the 'Reply' and 'ReplyAll' behaviour as well. 

I tried implementing the QuickActionDefaultsHandler (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_QuickAction_QuickActionDefaultsHandler.htm) without any success, and am interested to understand from you how this can be achieved. 

Thank you, 
Ido. 
 
Ido Greenbaum 10Ido Greenbaum 10
I was able to achieve that, implementing the 'QuickActionDefaultsHandler'. 'Reply' and 'ReplyAll' is being set with the appropriate Email Template. 
m h 9m h 9
Hi Ido..
Please help me in implementing this functioality.
I tried using 'QuickActionDefaultsHandler' but I think reply all and reply button is not even calling this interface..as I used debug statements but did not see any thing in debug..please help me
 
Ido Greenbaum 10Ido Greenbaum 10
Sure. 
If you can post here your code, I'll be able to help in debugging. 
m h 9m h 9
Thanks for the quick response.I got my answer ..actually this interface is for case feed only and I was expecting it in case detail pagereply and reply all buttons.Is there any way to give me the Test class for this or any direction how to write test class for this.All help is highly appreciated Thanks and RegardsMarshal
Ido Greenbaum 10Ido Greenbaum 10
Hi Marshal, 
Below is the test class, for the 'EmailPublisherLoader': 
 
@isTest
private class EmailPublisherLoaderTest {
    static Case myCase {get;set;}
    static EmailMessage myMsg {get;set;}
    
    static testmethod void EmailPublisherLoader_NoReplyToId() {
        Exception failureDuringExecution = null;
        init();
        init2();
        
        //create QuickActionDefaults
        List<Map<String, Object>> defaultSettingAsObject = new List<Map<String, Object>>
        {
          new Map<String, Object>
          {
                'targetSObject' => new EmailMessage(),
                'contextId' => myCase.Id,
                'actionType' => 'Email',
                'actionName' => 'Case.Email',
                'fromAddressList' => new List<String> { 'salesforce@test.com' }
          }
        };

        List<QuickAction.SendEmailQuickActionDefaults> defaultsSettings = 
            (List<QuickAction.SendEmailQuickActionDefaults>)JSON.deserialize(JSON.serialize(defaultSettingAsObject), List<QuickAction.SendEmailQuickActionDefaults>.class);
        Test.startTest();
        try {
            (new EmailPublisherLoader()).onInitDefaults(defaultsSettings);
        }
        catch(Exception e) {
            failureDuringExecution = e; 
        }

        Test.stopTest();
        System.assertEquals(null, failureDuringExecution, 'There was an exception thrown during the test!');
    }
    static testmethod void EmailPublisherLoader_WithReplyToId() {
        Exception failureDuringExecution = null;
        init();
        init2();
        
        //create QuickActionDefaults
        List<Map<String, Object>> defaultSettingAsObject = new List<Map<String, Object>>
        {
          new Map<String, Object>
          {
                'targetSObject' => new EmailMessage(),
                'replyToId' => myMsg.Id,
                'contextId' => myCase.Id,
                'actionType' => 'Email',
                'actionName' => 'Case.Email',
                'fromAddressList' => new List<String> { 'salesforce@test.com' }
          }
        };

        List<QuickAction.SendEmailQuickActionDefaults> defaultsSettings = 
            (List<QuickAction.SendEmailQuickActionDefaults>)JSON.deserialize(JSON.serialize(defaultSettingAsObject), List<QuickAction.SendEmailQuickActionDefaults>.class);
        Test.startTest();
        try {
            (new EmailPublisherLoader()).onInitDefaults(defaultsSettings);
        }
        catch(Exception e) {
            failureDuringExecution = e; 
        }

        Test.stopTest();
        System.assertEquals(null, failureDuringExecution, 'There was an exception thrown during the test!');
    }
    
    static void init(){
        myCase = 
            new Case(
                Status='Status'
                , Origin='Email'
                , Reason = 'Reason'
                , RecordTypeId = '01261000000XfOl'
            );
        insert myCase;
        
        myMsg = 
            new EmailMessage(
                ParentId = myCase.Id
            );
        insert myMsg;
    }
     static void init2(){
        myCase = 
            new Case(
                Status='Status'
                , Origin='Email'
                , Reason = 'Reason'
                , RecordTypeId = '01261000000XfOq'
            );
        insert myCase;
        
        myMsg = 
            new EmailMessage(
                ParentId = myCase.Id
            );
        insert myMsg;
    }
    @isTest(seeAllData=true)
static void test_mockIfAtAllPossible(){
}
@future
public static void futureLeadReassign(Set<ID> lIdReassignSet) { 

    List<Lead> lUpdList = [SELECT Id FROM Lead WHERE Id IN: lIdReassignSet];
    for (Lead l:lUpdList) {
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule = true;                                         // use leadAssignment rules when updating
        l.setOptions(dmo);
    }

    try {update(lUpdList);}
    catch (DmlException e) {/* do something here like send email to sysad with error */}
}
}

 
m h 9m h 9
Thanks again for quick reply IdoI got the test class and it really helped me a lot to get me started.but I again get stuck in test class. actually my requirement is to assign template id based on profile.for example if the profileid is XYZ then assign' ab' template id and if not then assign different template id.I took your code as base and modified according to my requirement .but the problem is if I change sysadmin profile to 'ab' and hadcode in my test class it is working fine, but for any other user if I hardcode or create new user in testdata setup it is not working ..I already wasted 2 days to figure out..but stuck here.Please help me 
m h 9m h 9
Actually IdoIf I am running my test class as sysadmin it is working as expected and If I am creating a new user with another profile or existing user with another profile then it is not covering the code in if condition that isif(userinfo.getprofileid='xyz')do this.if I give view all data on case object to this xyz profile then it is working ...but I am not sure if I need to give view all data permission on case object for the test case to pass or we have any other solution for thisReally appreciate all your helpThanks and RegardsMarshal On Sunday, July 31, 2016 2:47 PM, Marshal Khan wrote: Thanks again for quick reply IdoI got the test class and it really helped me a lot to get me started.but I again get stuck in test class. actually my requirement is to assign template id based on profile.for example if the profileid is XYZ then assign' ab' template id and if not then assign different template id.I took your code as base and modified according to my requirement .but the problem is if I change sysadmin profile to 'ab' and hadcode in my test class it is working fine, but for any other user if I hardcode or create new user in testdata setup it is not working ..I already wasted 2 days to figure out..but stuck here.Please help me 
Ido Greenbaum 10Ido Greenbaum 10
Hi Marshal, 
Not sure I fully understand you new issue. The test code is destined to pass all test while deploying the code into production. 
Can you please post here both your Apex Class implementation and the Test Class you have written? 

Ido.