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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Trigger to create standard HTML EmailTemplate

Hi,

 

Does anybody know how to create a HTML Email template using a trigger or Apex class. I have tried creating a EmailTemplate using trigger but it throws an Mixed DML exception.

 

I am not able to figure out the solution for the same. 

 

Kindly give an solution ASAP.

 

 

sfdcfoxsfdcfox

You can't mix Setup and non-Setup DML operations. For example, you can't create a Salesforce user in a trigger and modify data. Instead, make your HTML creation code a @future operation so it runs asynchronously. If that won't work, then maybe you need to reconsider what you're trying to do-- maybe you don't need email templates at all.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Since we have a visual force Email Editor we need to save the template in standard Email Templates once the record is created as a custom object.

 

Can we create @future operations in a trigger???

sfdcfoxsfdcfox

You can call a future method from basically anywhere except another future method (see @future annotation in the docs). It works like this:

 

trigger x on obj (after insert) {
    // do stuff with obj here
    myfutureclass.futuremethod(trigger.newmap.keyset());
}

Your future method can then do additional stuff:

 

public class myfutureclass {
   @future
   public static void futuremethod(set<id> objs) {
      // retrieve obj list, create templates, and save.
   }
}