+ 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 

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

    public pageReference send(){

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

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

            emailTemplatebody.body = body;

         // update emailTemplatebody;
            Messaging.SendEmailResult [] res = Messaging.SendEmail(new Messaging.SingleEmailMessage[] {email});      
            for ( Messaging.sendEmailResult result : res ) {
                   if ( !res[0].isSuccess () ) {
                       System.debug ( result  );
                       ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email Sent Successfully' );
            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.

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