Skip to main content
Alok Ranjan (CNX) 님이 #Ask Salesforce Anything에 글을 올렸습니다

Hi,

I am exploring CASE using salesforce lightning. I want a feature to be implemented for auto draft creation when i am opening any case details and trying to response the email. The draft should be automatically rendered to the email and retain all email thread.

 

To get this feature i have enabled 'Enable default email templates' in support setting and selected my new apex class to be executed to pick/choose the template and render in the email automatically. But template is not rendering.

 

So I need a guidance to achieve this implementation.

 

My apex class is as following:

 

global without sharing class SmartTemplateLoader implements Support.EmailTemplateSelector {

    // Empty constructor

    global SmartTemplateLoader() {    }

    // 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

        Case c = [SELECT Subject, Description FROM Case WHERE Id=:caseId];

        System.debug('Case Subject is: ' + c.Subject);

        Smart_Template_Loader__c conf = null;

        List<Smart_Template_Loader__c> confs = null;

        if (c.Subject.contains('Password unlock')) {

            String subject = '\''+ c.Subject + '\'';            

            String query = 'SELECT Template_Name__c FROM Smart_Template_Loader__c WHERE Case_Subject__c ='  + subject;

            System.debug('query is: ' + query);            

            confs = Database.query(query);//[SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE (Case_Subject__c ='Generic subject')];

            System.debug('confs is: ' + confs);

            if (!confs.isEmpty()) {

            conf = confs[0];

            System.debug('got template: ' + conf.Template_Name__c);

        }

        } else if (c.Subject.contains('Password reset')) {

            String subject = '\''+ c.Subject + '\'';            

            String query = 'SELECT Template_Name__c FROM Smart_Template_Loader__c WHERE Case_Subject__c ='  + subject;

            System.debug('query is: ' + query);            

            confs = Database.query(query);//[SELECT Template_Name__c FROM Smart_Template_Loader_Conf__c WHERE (Case_Subject__c ='Generic subject')];

            System.debug('confs is: ' + confs);

            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];

        //Return the ID of the template selected

        if (!ets.isEmpty()) {

            EmailTemplate et = ets[0];

            System.debug('Template is: ' + et);

            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;

    }

}

 

Please, help me in this regards.

 

Alok Ranjan

0/9000