Skip to main content
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 ) );
    }
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<String, String> data = new Map<String, String>();
for(String key:new List<String> {'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));
}
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 ) );
    }
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<String, String> data = new Map<String, String>();
for(String key:new List<String> {'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));
}
added 68 characters in body
Source Link
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 ) );
    }

Unfortunately it does not for email templates with letter heads.

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 ) );
    }

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 ) );
    }

Unfortunately it does not for email templates with letter heads.

Source Link
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 ) );
    }