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
sekhar maramreddysekhar maramreddy 

how to create emailtemplate dynamically in salesforce through code

how to create emailtemplate dynamically in salesforce through code
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sekhar,
// Constructor to populate instance of your object
    public Cls_SendEmail(ApexPages.StandardController controller) {

        this.order = (Order__c )controller.getRecord();
        order = [SELECT name, ContactEmail__c , case__r.contact.email, Case__r.Case_Subject__C FROM Order__c
             WHERE id = :ApexPages.CurrentPage().getParameters().get('id')];
        to =  order.case__r.contact.email;

        emailTemplatebody = [Select id, subject, body from EmailTemplate where 
                                            DeveloperName=:'ET_EmailInvoice'];

        subject = emailTemplatebody.subject; 
        body =  emailTemplatebody.body; 
        order.EmailBodyFromAgent__c = body;                          
    }

    public pageReference send(){

            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();  
            String[] toaddress = new String[]{};    
            toaddress.add(to);
            email.setToAddresses(toaddress); //Use SOQL to retrieve addresses in the address            
            email.setBccSender(true);

            If ( cc != null )
            {
                String[] ccaddress = new String[]{};    
                ccaddress.add(cc);
              //  email.setCcAddresses(ccaddress);
            }

            email.setTargetObjectId(order.case__r.contact.Id);
            email.setWhatId(order.Id); 
            emailTemplatebody.body = body;

         // update emailTemplatebody;
            email.setTemplateId(emailTemplatebody.id);
            email.setSaveAsActivity(true);
            Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});      
            for ( Messaging.sendEmailResult result : res ) {
                   if ( !res[0].isSuccess () ) {
                       System.debug ( result  );
                   }
                   else{
                       ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
                       ApexPages.addMessage(msg);
                   }
               }  
            return null;

        }
May I suggest you to Please refer the below link for reference. hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
Deepali KulshresthaDeepali Kulshrestha
Hi Sekhar, 

I have gone through your question. You can create the email template dynamically through Apex Code. 
The given below can be used for test class and if you want to use for a simple apex class remove the system.runAs(user) line.

Please go through the code given below.


 public static String  createEmailTemplate(){
        EmailTemplate validEmailTemplate;
        System.runAs (new User(Id = UserInfo.getUserId()) ){
            validEmailTemplate = new EmailTemplate();
            validEmailTemplate.isActive = true;
            validEmailTemplate.Name = 'name';
            validEmailTemplate.DeveloperName = 'unique_name_addSomethingSpecialHere';
            validEmailTemplate.TemplateType = 'text';
            validEmailTemplate.FolderId = UserInfo.getUserId();

            insert validEmailTemplate;
        }
        return validEmailTemplate.ID;
    }


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com