Skip to main content
1 of 3
Patlatus
  • 17.8k
  • 13
  • 82
  • 191

I had some other idea to do this.

EmailTemplate template = [ SELECT Id, Name, Subject, Body, HtmlValue FROM EmailTemplate WHERE Name = 'My Name']; templateId = template.Id; Custom_Object__c obj = [ SELECT Name, Custom_Field_1__c, Custom_Field_2__c FROM Custom_Object__c WHERE Id = :my.Id]; Map data = new Map(); for ( String key : new List{'Name', 'Custom_Field_1__c', 'Custom_Field_2__c' } ) { data.put('{!Custom_Object__c.' + key + '}', (String) obj.get(key) ); System.debug( LoggingLevel.ERROR,'@@@ key : ' + key ); System.debug( LoggingLevel.ERROR,'@@@ obj.get(key) : ' + obj.get(key) ); } data.put('User.FirstName', UserInfo.getFirstName() ); data.put('User.LastName', UserInfo.getLastName() ); String body = template.HtmlValue; System.debug( LoggingLevel.ERROR, body); System.debug( LoggingLevel.ERROR,'@@@ template.HtmlValue: ' + template.HtmlValue ); System.debug( LoggingLevel.ERROR,'@@@ template.Subject: ' + template.Subject );
    String subj = template.Subject;
    for ( String key : data.keySet() ) {
        body = body.replace( key, data.get( key ) );
        subj = subj.replace( key, data.get( key ) );
    }
Patlatus
  • 17.8k
  • 13
  • 82
  • 191