You need to sign in to do that
Don't have an account?
Michael M
Email send class not working
Hello, I have a class that is supposed to send a VF template. The debug logs are saying it is sent, but the email is not going out. Can someone help to fix my email class? What am I missing?
CLASS
Global class LeadOwnerEmail implements Schedulable{
Global void execute(SchedulableContext SC) {
List<User> uList = [select id, email from user where id in (select ownerid from community_lead__c)];
List<Id> listids= new List<Id>();
for(User u :uList) {
EmailTemplate et=[Select id from EmailTemplate where name = 'LeadOwnerEmail'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(u.id);
mail.setSenderDisplayName('Lead Ownership');
mail.setTemplateId(et.id);
mail.setSaveAsActivity(false);
String[] toAddresses = new String[] {u.email};
mail.setToAddresses(toAddresses);
system.debug('**To Addresses: ' + u.email);
//mail.setReplyTo(liaisonEmail);
try {
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('**SENT!!!');
return;
} catch (Exception e) {
System.debug(e.getMessage());
}
}
}
}
VF EMAIL
<messaging:emailTemplate subject="Your Team's Leads- Owner Assignments" recipientType="User" relatedToType="User">
<messaging:htmlEmailBody >
Hi {!RelatedTo.firstName},
<p>The following leads have not yet been updated:</p>
<c:leadsinfo ToID="{!RelatedTo.Id}" />
<p>To update these leads, click on the "Lead Status" field located in the upper right on the lead page layout.</p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
CLASS
Global class LeadOwnerEmail implements Schedulable{
Global void execute(SchedulableContext SC) {
List<User> uList = [select id, email from user where id in (select ownerid from community_lead__c)];
List<Id> listids= new List<Id>();
for(User u :uList) {
EmailTemplate et=[Select id from EmailTemplate where name = 'LeadOwnerEmail'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(u.id);
mail.setSenderDisplayName('Lead Ownership');
mail.setTemplateId(et.id);
mail.setSaveAsActivity(false);
String[] toAddresses = new String[] {u.email};
mail.setToAddresses(toAddresses);
system.debug('**To Addresses: ' + u.email);
//mail.setReplyTo(liaisonEmail);
try {
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('**SENT!!!');
return;
} catch (Exception e) {
System.debug(e.getMessage());
}
}
}
}
VF EMAIL
<messaging:emailTemplate subject="Your Team's Leads- Owner Assignments" recipientType="User" relatedToType="User">
<messaging:htmlEmailBody >
Hi {!RelatedTo.firstName},
<p>The following leads have not yet been updated:</p>
<c:leadsinfo ToID="{!RelatedTo.Id}" />
<p>To update these leads, click on the "Lead Status" field located in the upper right on the lead page layout.</p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
In addition to that, soql query on Emailtemplate needs to move out of the loop as well.
All Answers
System.LimitException: Too many Email Invocations: 11". Any tips for what I can write in my code to fix this?
Here's the current code:
Global class LeadOwnerEmail implements Schedulable{
Global void execute(SchedulableContext SC) {
List<User> uList = [select id, email from user where id in (select ownerid from community_lead__c)];
List<Id> listids= new List<Id>();
List<string> listemails = new List<string>();
for (user uu : uList){
listids.add(uu.id);
listemails.add(uu.email);
}
for(User u :uList) {
EmailTemplate et=[Select id from EmailTemplate where name = 'LeadOwnerEmail'];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
mail.setTargetObjectId(u.id);
mail.setSenderDisplayName('Lead Ownership');
mail.setTemplateId(et.id);
mail.setSaveAsActivity(false);
// String[] toAddresses = new String[] {u.email};
//mail.setToAddresses(listemails);
system.debug('**To Addresses: ' + u.email);
//mail.setReplyTo(liaisonEmail);
try {
Messaging.sendEmail(new Messaging.SingleEmailMessage [] { mail });
system.debug('**SENT!!!');
// return;
} catch (Exception e) {
System.debug(e.getMessage());
}
}
}
}
In addition to that, soql query on Emailtemplate needs to move out of the loop as well.
Global class LeadOwnerEmail implements Schedulable{
Global void execute(SchedulableContext SC) {
List<User> uList = [select id, email from user where id in (select ownerid from community_lead__c)];
List<Id> listids= new List<Id>();
List<string> listemails = new List<string>();
for (user uu : uList){
listids.add(uu.id);
listemails.add(uu.email);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
EmailTemplate et=[Select id from EmailTemplate where name = 'LeadOwnerEmail'];
for(User u :uList) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
mail.setTargetObjectId(u.id);
mail.setSenderDisplayName('Lead Ownership');
mail.setTemplateId(et.id);
mail.setSaveAsActivity(false);
// String[] toAddresses = new String[] {u.email};
//mail.setToAddresses(listemails);
system.debug('**To Addresses: ' + u.email);
emails.add(mail);
//mail.setReplyTo(liaisonEmail)
if (emails.size() > 0) {
Messaging.sendEmail(emails,false);
system.debug('**SENT!!!');
}
}
}
}}
I have done that change to your code. If this solves your question, mark this as solved.
1) It sends multiple emails per user. I only want it to send 1 email per user.
2) The chart on the VF Template is blank. When I test send the email from the VF template, the chart is full of data, but in these emails it is coming up blank.
Any idea how I can fix these?
VF TEMPLATE
<messaging:emailTemplate subject="Your Team's Leads- Owner Assignments" recipientType="User" relatedToType="User">
<messaging:htmlEmailBody >
Hi {!RelatedTo.firstName},
<p>The following leads have not yet been updated:</p>
<c:leadsinfo ToID="{!RelatedTo.Id}" />
<p>To update these leads, click on the "Lead Status" field located in the upper right on the lead page layout.</p>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
COMPONENT
(This is saved as leadsinfo.vfc)
<apex:component controller="LeadsInfoController" access="global">
<apex:attribute name="ToID" type="ID" description="the lead owner ID" assignTo="{!salesRepID}"/>
<apex:datatable value="{!InactiveLeads}" var="item" border="1">
<apex:column headervalue=" Name ">
<apex:outputText value="{!item.name}"/>
</apex:column>
<apex:column headervalue=" Created Date ">
<apex:outputText value="{!item.CreatedDate}"/>
</apex:column>
</apex:datatable>
</apex:component>
CONTROLLER
public class LeadsInfoController {
//capture the user id
public ID salesRepID {get; set;}
public List<community_lead__c> ClsInfo = new List<community_lead__c>();
public List<community_lead__c> getInactiveLeads() {
ClsInfo = [select Name, CreatedDate from community_lead__c where ownerId =: salesRepID];
return ClsInfo;
}
}
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
EmailTemplate et=[Select id from EmailTemplate where name = 'LeadOwnerEmail'];
for(User u :uList) {
Id userid = u.id;
//Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(et.id, u.id, u.id);