Get Id from Email Template through Apex Salesforce

 public static void sendingMail(List<Case_Comment__c> cseCmntLst){

        system.debug('cseCmntLst>>>'+cseCmntLst);

        if(!cseCmntLst.isEmpty()){

            List<Case_Comment__c> lstCmntCase = [SELECT Id,Contact__c,Related_Contact_Email__c FROM Case_Comment__c WHERE Id =:cseCmntLst[0].Id];   

            List<String> lstEmail = new List<String>();

            if(String.isNotBlank(lstCmntCase[0].Related_Contact_Email__c)){

                lstEmail.add(lstCmntCase[0].Related_Contact_Email__c);

            }

            System.debug('TO Email>>>>>>>'+lstEmail); 

            if(!lstEmail.isEmpty()){

                EmailTemplate emTemp = [SELECT Id FROM EmailTemplate WHERE DeveloperName='NotifyCustomerEmailTemplate'];

                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

                message.setToAddresses(lstEmail);

                message.setWhatId(lstCmntCase[0].Id);

                message.setTargetObjectId(lstCmntCase[0].Contact__c);

             

                if(String.isNotBlank(emTemp.Id)){

                    message.setTemplateId(emTemp.Id);

                }else{

                    message.setPlainTextBody('No Email template found.');

                }

                Messaging.SendEmailResult[] results = Messaging.sendEmail( new List<Messaging.SingleEmailMessage> {message} );

                System.debug('Mail Sent'+results);

            }

        }

Comments

Popular posts from this blog

Custom List View Button In Salesforce

Get Record Id RecordId in LWC

PDF LWC (Link for pdf liabrary)