Skip to main content
fixing syntax
Source Link
ebt
  • 2.8k
  • 1
  • 21
  • 26
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate[] tpls = [SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template];
            EmailTemplates et = null;
            if(!tpls.isEmpty() == true){
                return "";
            }
            et = tpls.get(0);
    return merge_template(data,ettpls.get(0);
}
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate[] tpls = [SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template];
            EmailTemplates et = null;
            if(!tpls.isEmpty() == true){
                return "";
            }
            et = tpls.get(0);
    return merge_template(data,et);
}
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate[] tpls = [SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template];
    if(tpls.isEmpty() == true){
        return "";
    }
    return merge_template(data,tpls.get(0);
}
added 143 characters in body
Source Link
ebt
  • 2.8k
  • 1
  • 21
  • 26
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplateEmailTemplate[] ettpls = (EmailTemplate) UtilsList.getFirst([SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template]template];
            EmailTemplates et = null;
            if(!tpls.isEmpty() == true){
                return "";
            }
            et = tpls.get(0);
    return merge_template(data,et);
}
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate et = (EmailTemplate) UtilsList.getFirst([SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template]);
    return merge_template(data,et);
}
public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate[] tpls = [SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template];
            EmailTemplates et = null;
            if(!tpls.isEmpty() == true){
                return "";
            }
            et = tpls.get(0);
    return merge_template(data,et);
}
updated with code example.
Source Link
ebt
  • 2.8k
  • 1
  • 21
  • 26

When accessing templates and needing multiple objects I will

  • select the template from the database
  • define a merge syntax, use regular expressions "{!([A-z_]+)}" to get a key name
  • pass a map<String,String> of data to replace the key (if exists) with data

you now have a template which is just a string. you can push the template to a VF page with <apex:outputText escape="false" value="{!template1}"/> or send an email manually in code.

code example:

public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate et = (EmailTemplate) UtilsList.getFirst([SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template]);
    return merge_template(data,et);
}

When accessing templates and needing multiple objects I will

  • select the template from the database
  • define a merge syntax, use regular expressions "{!([A-z_]+)}" to get a key name
  • pass a map<String,String> of data to replace the key (if exists) with data

you now have a template which is just a string. you can push the template to a VF page with <apex:outputText escape="false" value="{!template1}"/> or send an email manually in code.

When accessing templates and needing multiple objects I will

  • select the template from the database
  • define a merge syntax, use regular expressions "{!([A-z_]+)}" to get a key name
  • pass a map<String,String> of data to replace the key (if exists) with data

you now have a template which is just a string. you can push the template to a VF page with <apex:outputText escape="false" value="{!template1}"/> or send an email manually in code.

code example:

public static Map<String,String> merge_template(Map<String,String> obj, EmailTemplate email){
    Map<String,String> data = new Map<String,String>();
    String html = email.HtmlValue;
    if(html!=null){
        html = m(html,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(html));
        data.put('HtmlValue',html);
    }
    else{
        data.put('HtmlValue','');
    }
    String body = email.Body;
    String subject = email.Subject;
    body = m(body,obj,Pattern.compile('(\\{![A-Za-z_]+\\})').matcher(body));
    
    data.put('Body',body);
    
    return data;
    
}
private static String m(String body,Map<String,String> obj,Matcher m){
    while(m.find()){
      String merge_field = m.group(0);
      String field = merge_field.substring(1,merge_field.length()-1);
      String value;
      try{
        value = (String) obj.get(field);
      }
     catch(Exception e){
        value='';
     }
     if(value!=null){
        body = body.replace(merge_field,value);
     }
     else{
        body = body.replace(merge_field,'');
     }
    }
    return body;
}

public static Map<String,String> build(String template,Map<String,String> data){
    EmailTemplate et = (EmailTemplate) UtilsList.getFirst([SELECT HtmlValue,Body,Subject FROM EmailTemplate WHERE DeveloperName = :template]);
    return merge_template(data,et);
}
Source Link
ebt
  • 2.8k
  • 1
  • 21
  • 26
Loading