You need to sign in to do that
Don't have an account?
Chinmoy 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});
}
}
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});
}
}
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
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>
Try this:
FIRST CREATE THIS NEW CLASS:
Then...
REPLACE YOUR TRIGGER CODE WITH... Let me know if it works. Regards
I dont know what to do?