+ Start a Discussion
Chinmoy DebnathChinmoy Debnath 

Email Trigger Not Working

I Designed this trigger to send emails to those who register in object="Bill__c" with custom template="Billa"
I cant see any thing wrong with it, But it isnt working...

trigger MailT on Bill__c (after insert) {
   Bill__c inquery = trigger.new[0]; 
  String[] toAddresses = new String[] {inquery.Email_Id__c}; 
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

  mail.setTargetObjectId(inquery.Id);
  mail.setSenderDisplayName('Salesforce Support');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);

 if (Trigger.isInsert) { 
 
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Billa'];
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   
     
   }
}
Rodrigo RESENDIZRodrigo RESENDIZ
Hi Chinmoy Debnath!

First of all, Is it absolutely necessary for you to use a template?
If so, in order to use a template you have to set the object with which the template will be filled,so you should use the  setWhatId(whatId) method. It is important for you to know that the setTargetObjectId() is used to set the ID of the contact, lead, or user to which the email will be sent. If you don't have this info available on your object then using a template isn't the best option.

Otherwise, you can build your own HTML and set it to your mail with the setHtmlBody(htmlBody) method. Also, you might add the emails addresses to which you are sending the email with the setToAddresses(toAddresses) method.
This way it is way easier than adding a template.

Finally, I would recommend you to use a future method in an independet service class, only if you don't need the mail to be sent inmmediately, in order to let the trigger flow continue.
For further info check the SingleEmailMessage documentation https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm
Hope this helps you. If you can use HTML body to create your email let me know and I can share a sample code to accomplish this requirement.
Regards
Chinmoy DebnathChinmoy Debnath
+Rodrigo RESENDIZ 
Yes I can use HTML template I have designed one but how to attach its name="Billa"

<!DOCTYPE>
<html>
<head>
<style>
#top{background-color:#EBEBEB;}
#top h1{float:right;margin-right:40%;}
#body{width:90%;margin:10px auto;}
#body h3{}
table#tab {
    width: 100%; 
    background-color: #C3EBBC;
}
#footer{text-align:center;
 position:absolute;
   bottom:0;
   width:100%;
   height:50px;   /* Height of the footer */
   background:#EBEBEB;}
#footer p{font-size:15px; line-height: 50%;}
#container {
   min-height:100%;
   position:relative;
}
</style>
</head>
<body>
<div id="container">
<div id ="top">
<img src="https://s-media-cache-ak0.pinimg.com/236x/4d/ca/85/4dca852427aa341216f869178e3e963b.jpg" height="100px"/>
<h1>Elecrical Solutions</h1>
</div>
<div id="body">
<h3>Electricity Bill</h3>
<p>Hi {!Bill__c.Consumer_Name__c},</p>
<p>Your Electricity Bill of Previous month:</p>
<table id="tab">
  <tr>
    <td>Billing Start Date</td>
    <td>{!Bill__c.Billing_Start_Date__c}</td> 
    
  </tr>
  <tr>
    <td>Billing End Date</td>
    <td>{!Bill__c.Billing_End_Date__c}</td>
  </tr>
  <tr>
    <td>Previous Month</td>
    <td>${!Bill__c.Bill_Amonut__c}</td>
  </tr>
  <tr>
    <td>Bill Due</td>
    <td>${!Bill__c.Bill_Due__c}</td>
  </tr>
  <tr>
    <td>Penalty</td>
    <td>${!Bill__c.Penalty__c}</td>
  </tr>
   <tr>
    <td>Total Bill</td>
    <td>${!Bill__c.Total_Bill_Amount__c}</td>
  </tr>
</table>
<p><b>So your Total Bill is ${!Bill__c.Total_Bill_Amount__c}</b></p>
</div>
<div id ="footer">
<p>© Copyright 1999-2016 by Refsnes Data.</p> 
<p>All Rights Reserved.Powered by Electrical Solutions</p>
</div>
</div>
</body>
</html>
 
Rodrigo RESENDIZRodrigo RESENDIZ
Chinmoy Debnath:

Try this:
FIRST CREATE THIS NEW CLASS:
public class BillTriggerHelper{
        public static Messaging.SingleEmailMessage createMail(Bill__c inquery){
            String[] toAddresses = new String[] {inquery.Email_Id__c}; 
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            
            
            String htmlBody = String.format('<!DOCTYPE> <html> <head> <style> #top{background-color:#EBEBEB;} #top h1{float:right;margin-right:40%;} #body{width:90%;margin:10px auto;} #body h3{} table#tab { width: 100%; background-color: #C3EBBC; } #footer{text-align:center; position:absolute; bottom:0; width:100%; height:50px; /* Height of the footer */ background:#EBEBEB;} #footer p{font-size:15px; line-height: 50%;} #container { min-height:100%; position:relative; } </style> </head> <body> <div id="container"> <div id ="top"> <img src="https://s-media-cache-ak0.pinimg.com/236x/4d/ca/85/4dca852427aa341216f869178e3e963b.jpg" height="100px"/> <h1>Elecrical Solutions</h1> </div> <div id="body"> <h3>Electricity Bill</h3> <p>Hi {0},</p> <p>Your Electricity Bill of Previous month:</p> <table id="tab"> <tr> <td>Billing Start Date</td> <td>{1}</td> </tr> <tr> <td>Billing End Date</td> <td>{2}</td> </tr> <tr> <td>Previous Month</td> <td>${3}</td> </tr> <tr> <td>Bill Due</td> <td>${4}</td> </tr> <tr> <td>Penalty</td> <td>${5}</td> </tr> <tr> <td>Total Bill</td> <td>${6}</td> </tr> </table> <p><b>So your Total Bill is ${7}</b></p> </div> <div id ="footer"> <p>© Copyright 1999-2016 by Refsnes Data.</p> <p>All Rights Reserved.Powered by Electrical Solutions</p> </div> </div> </body> </html></div></body></html>'
                                            , new List<String>{inquery.Consumer_Name__c,inquery.Billing_Start_Date__c.format(), inquery.Billing_End_Date__c,inquery.Bill_Amonut__c, inquery.Bill_Due__c, inquery.Penalty__c, inquery.Total_Bill_Amount__c, inquery.Total_Bill_Amount__c});
            mail.setToAddresses(toAddresses);
            mail.setHtmlBody(htmlBody);
            return mail;
        }
    }

Then...
REPLACE YOUR TRIGGER CODE WITH...
if (Trigger.isInsert) {
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        //Bulkify in case you insert more tan one Bill
        for(Bill__c bll : trigger.new){
            mails.add(BillTriggerHelper.createMail(bll));
        }
        if(!mails.isEmpty()){
            Messaging.SendEmailResult [] r = Messaging.sendEmail(mails);
        }
    }
Let me know if it works. Regards
 
Chinmoy DebnathChinmoy Debnath
+Rodrigo RESENDIZ thanks for your help but there is a error in class ERROR: "initial expression of incorrect type expected string" 
I dont know what to do?
Rodrigo RESENDIZRodrigo RESENDIZ
It can be caused by any of your custom fields from Bill__c object. One of them can't be parsed to String... try this:
public class BillTriggerHelper{
        public static Messaging.SingleEmailMessage createMail(Bill__c inquery){
            String[] toAddresses = new String[] {inquery.Email_Id__c+''}; 
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            
            
            String htmlBody = String.format('<!DOCTYPE> <html> <head> <style> #top{background-color:#EBEBEB;} #top h1{float:right;margin-right:40%;} #body{width:90%;margin:10px auto;} #body h3{} table#tab { width: 100%; background-color: #C3EBBC; } #footer{text-align:center; position:absolute; bottom:0; width:100%; height:50px; /* Height of the footer */ background:#EBEBEB;} #footer p{font-size:15px; line-height: 50%;} #container { min-height:100%; position:relative; } </style> </head> <body> <div id="container"> <div id ="top"> <img src="https://s-media-cache-ak0.pinimg.com/236x/4d/ca/85/4dca852427aa341216f869178e3e963b.jpg" height="100px"/> <h1>Elecrical Solutions</h1> </div> <div id="body"> <h3>Electricity Bill</h3> <p>Hi {0},</p> <p>Your Electricity Bill of Previous month:</p> <table id="tab"> <tr> <td>Billing Start Date</td> <td>{1}</td> </tr> <tr> <td>Billing End Date</td> <td>{2}</td> </tr> <tr> <td>Previous Month</td> <td>${3}</td> </tr> <tr> <td>Bill Due</td> <td>${4}</td> </tr> <tr> <td>Penalty</td> <td>${5}</td> </tr> <tr> <td>Total Bill</td> <td>${6}</td> </tr> </table> <p><b>So your Total Bill is ${7}</b></p> </div> <div id ="footer"> <p>© Copyright 1999-2016 by Refsnes Data.</p> <p>All Rights Reserved.Powered by Electrical Solutions</p> </div> </div> </body> </html></div></body></html>'
                                            , new List<String>{inquery.Consumer_Name__c+'',inquery.Billing_Start_Date__c+'', inquery.Billing_End_Date__c+'',inquery.Bill_Amonut__c+'', inquery.Bill_Due__c+'', inquery.Penalty__c+'', inquery.Total_Bill_Amount__c+'', inquery.Total_Bill_Amount__c+''});
            mail.setToAddresses(toAddresses);
            mail.setHtmlBody(htmlBody);
            return mail;
        }
    }
Regards