+ Start a Discussion
sales@myvarmasales@myvarma 

i wrote trigger and email messaging when ever we insert or update contact fields .email must be created in apex instead off calling existing templates names

  EmailTemplate et=[Select id from EmailTemplate where name=:'Sales: New Customer Email'];


with out calling like this how to  give standar template to present mail update progrm


help me in this prgrm

 
trigger SendEmailToAccount on Contact (after insert,after update) 
{
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert )
        { 
           
            HelperContactTrigger.sendEmail(trigger.new);
        }
    }
        if(trigger.isAfter && trigger.isUpdate )
        {           
         HelperContactTrigger.sendEmailafter(trigger.new);
        }
}
 
public with sharing class HelperContactTrigger {
    public static List<Contact> sendEmail(List<Contact>Contacts)
    {
     
        
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();   
        
        for(Contact con : Contacts)
        {
         
            if(con.AccountId != null && con.Email != null){

                                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

              
                singleMail.setTargetObjectId(con.Id);
                singleMail.setSaveAsActivity(false);
     

singleMail.setHtmlBody('Your record:<b> ' + case.Id +' </b>has been created.<p>');
            
                emails.add(singleMail);
            }
        }
           
        Messaging.sendEmail(emails);

        return Contacts;          
        
    }

    public static List<Contact> sendEmailafter(List<Contact>Contacts)
    {
        
               List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();   
        
        for(Contact con : Contacts)
        { 
         
            if(con.AccountId != null && con.Email != null){

               
                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();

               
                singleMail.setTargetObjectId(con.Id);

                singleMail.setSaveAsActivity(false);

 singleMail.setHtmlBody('Your record:<b> ' + case.Id +' </b>has been updated.<p>');
              
               

                emails.add(singleMail);
            }
         }
           
        Messaging.sendEmail(emails);

        return Contacts;          
        }
    }

 
Gopal AgrawalGopal Agrawal

Hi,

If you do not want to use a standard template, then you have to create HTML body inside apex and then set it as Email body.

Like

  string htmlBodyStr ='<div style = "background-color:White; margin-left:10px; margin-right:10px; ">';

singleMail.setHtmlBody(htmlBodyStr );

PLease let me know , if it will help you.

sales@myvarmasales@myvarma
public with sharing class HelperContactTrigger {
    public static List<Contact> sendEmail(List<Contact>Contacts)
    {
     
        
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();   
        
        for(Contact con : Contacts)
        {
         
            if(con.AccountId != null && con.Email != null){
              
           

             Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
             string htmlBodyStr ='<div style = "background-color:White; margin-left:10px; margin-right:10px; ">';

              string subject ='information';   
            singleMail.setSubject(subject);
             singleMail.setHtmlBody(htmlBodyStr );
                singleMail.setTargetObjectId(con.Id);
                singleMail.setSaveAsActivity(false);
                             
                emails.add(singleMail);
            }
        }
           
        Messaging.sendEmail(emails);

        return Contacts;          
        
    }

    public static List<Contact> sendEmailafter(List<Contact>Contacts)
    {
        
               List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();   
        
        for(Contact con : Contacts)
        { 
         
            if(con.AccountId != null && con.Email != null){

                 
                Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
                 string htmlBodyStr ='<div style = "background-color:White; margin-left:10px; margin-right:10px; ">';
                  string subject ='information';
                        singleMail.setSubject(subject);
                    
               singleMail.setHtmlBody(htmlBodyStr );
                singleMail.setTargetObjectId(con.Id);

                singleMail.setSaveAsActivity(false);


              
               

                emails.add(singleMail);
            }
         }
           
        Messaging.sendEmail(emails);

        return Contacts;          
        }
    }

 
sales@myvarmasales@myvarma
not getting
Gopal AgrawalGopal Agrawal
Hi,

Please replace line 46 with

below code:

string htmlBodyStr ='<HTML  lang="ja"><body>'+
'<br><br>'+
'Dear'+ {!Contact.FirstName}',
'<br><br>'+
All of us at GenWatt are glad to have'+ {!Account.Name} +'as a customer.'+ 
'<br><br>'+
'I would like to introduce myself as your Account Manager. Should you have any questions, please feel free to call me at'+ {!User.Phone} +'or email me at'+ {!User.Email}+'. 
<br><br>'+
'You can also contact GenWatt on the following numbers:'+
'<br><br>'+
'Main: 810-545-1000'+
'<br><br>'+
'Sales: 810-545-1222'+
'<br><br>'+
'Customer Service & Support: 810-545-1333'+
'<br><br>'+
'Fax: 810-545-1001'+
'<br><br>'+
'I am confident GenWatt will serve you well and hope to see our relationship grow over the years.'+
'<br><br>'+
'Best Regards, '+
'<br>'+
{!User.Name}
'</body>'+
'<br>'+
'</HTML>'; 
VENKATA KRISHNA MOHANVENKATA KRISHNA MOHAN
Write a trigger on contact such that when ever a contact record is created u should update the email