0

I am trying to enable the default email template selection logic as per the salesforce provided document - https://help.salesforce.com/articleView?id=case_interaction_default_templates.htm&type=5

But I am not able to get the predefined email template in my case feed in salesforce classic . I have already selected Enable default email templates and my template selection logic class in support settings. Can some one guide me what is wrong here and how we can proceed

    global class MyCaseTemplateChooser implements Support.EmailTemplateSelector {
    // Empty constructor
    global MyCaseTemplateChooser() {    }

    // The main interface method
    global ID getDefaultEmailTemplateId(ID caseId) 
    {

        system.debug('Login User Role : '+userRole);
        // Select the case we're interested in, choosing any fields that are relevant to our decision
        Case c = [SELECT Subject, Description, Recordtype.Name, RecordTypeId,  FROM Case WHERE Id = :caseId];
        system.debug('CASE****'+c);
        EmailTemplate et;

        if (c.RecordType.Name.contains('Support Case') )
        {
            system.debug('Entered in If loop to select Template');
            et = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'MyTestTemplate'];
            system.debug('EmailTemplate*** :'+et.Name);
        }

        return et.id;       
    }
}
1

Your class should look more like the following:

global TemplateLoader() {    }

    // The main interface method
    global ID getDefaultEmailTemplateId(ID caseId) {
        // Select the case we're interested in, choosing any fields that are relevant to our decision
        Id CaseRecordTypeId = Your RecordTypeID Query Here; 
        Case c = [SELECT Status,Reason,RecordtypeId,Origin,Country__c FROM Case WHERE 
                 Id=:caseId and recordtypeid=:CaseRecordTypeId and Origin=:'Web' 
                      and Status=:'Assigned' and Country__c=:'Japan'];

        Smart_Template_Loader_Conf__c conf = null;
        List<Smart_Template_Loader_Conf__c> confs = null;

        // get template from case status + case reason
        confs = [SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE 
                Case_Status__c =: c.status and Case_Region__c =: c.Country__c];
        if (!confs.isEmpty()) {
            conf = confs[0];
            System.debug('got template: ' + conf.Template_Name__c);
        }

        if (conf==null) {
            System.debug('no template!');
            return null;
        }

        // get the template ID
        List<EmailTemplate> ets = [SELECT id FROM EmailTemplate WHERE
             DeveloperName=:conf.Template_Name__c];
        if (!ets.isEmpty()) {
            EmailTemplate et = ets[0];
            System.debug('template: ' + conf.Template_Name__c + ' -- id: ' + et.id);
            return et.id;
        }
        System.debug('No template with the name: ' + conf.Template_Name__c);        
        return null;
    }
  • Hi crmprogdev I tried this thing earlier but it is also not working. Any other suggestions ? – chousen basha Sep 19 at 11:09
  • @chousenbasha You need to adapt what I provided above to match your org. Don't expect to just "plug in" this code and have it work for you. Adapt it, then tell us what's not working. Without a more descriptive response or debug logs, I can't give you any guidance. – crmprogdev Sep 19 at 12:59
  • As per my code i just want to select the email template based on the record type of a case. While trying to reply to the case it should select the email template which is not happening even though after selecting the apex class in support settings for default email template. Any suggestions are welcomed – chousen basha Oct 1 at 10:03
  • And Also how to verify whether my class is calling or not . On which scenario my class will be called to select the template. I am able to create a case able to send the email. – chousen basha Oct 1 at 10:31
  • I think you want to see this page in the Help: help.salesforce.com/…. I recommend you create a unit test to verify the various conditions you're looking to validate. Your default loader class needs an override to do what you're asking about using a manual selection, an Action, Macro, or some other automation. – crmprogdev Oct 1 at 20:23

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.