You need to sign in to do that
Don't have an account?

Mass Email with Carbon Copies. Need help with code.
I have the following code that sends a mass email out based on a end date being past due. Being a very new apex developer I need some help with the following task. The main list of people receiving the mass email is setup in the getSPEEmailAddresses module. The problem I am having is that there is a public group defined in the getpublicgroupaddresse module that I need to cc on each email going out in the getSPEEmailaddresses module and I don't know how to go about doing that. Can anyone adjust the below code to accomplish that task?
/**************************************************************************/
//The SPEs past due Notice Emailer
//
//This class can be scheduled to run nightly to send emails to SPE
//owners, and the public group called SPE_Pastdue_Notification
// when the End DAte (End_Date__c) is within 14 days of being past due
// and the routing status = approved.
//
/***************************************************************************/
global class SPEs_Pastdue implements Schedulable{
//mandataory function called by the Apex Scheduler
//**************************************************
global void execute(SchedulableContext SC) {
//**************************************************
sendmail(); // our main function that does the email sending
}//end execute
//Get email addresses of people pastdue SPEs by 14 days
//**************************************************
public List<Id> getSPEEmailAddresses(Date CalDate) {
//**************************************************
List<Id> mailToIds = new List<Id>();
//find a list of SPEs with overdue end dates
Sales_Policy_Exceptions__c[] c = [SELECT Id, End_Date__C, Routing_Status__c, owner_email__c
FROM Sales_Policy_Exceptions__c
WHERE End_Date__c <= : CalDate
AND Routing_Status__c = 'Approved'
];
// Old Date samples
// DAY_IN_MONTH(CloseDate) = : Day
// AND CALENDAR_MONTH(CloseDate) = : Month
// AND CALENDAR_Year(CloseDate) = : Year
//add the list of Sales_Policy_Exceptions__c to a list
for(Sales_Policy_Exceptions__c recipient : c) {
System.Debug('\n*******Found Sales_Policy_Exceptions__c Recipient');
if (recipient.owner_Email__c != '')
{
mailToIds.add(recipient.Id); // add to email SPE array
System.Debug('\n*******Recipient: '+ recipient.owner_email__c);
} else {
//Just add to chatter array
System.Debug('\n*******NO Recipient');
} //end if
} //end for loop
return mailToIds; //return the list
} //end getSPEEmailAddresses()
//**************************************************
public List<String> getPublicGroupAddresses() {
//**************************************************
List<String> idList = new List<String>();
List<String> mailCCAddresses = new List<String>();
List<ID> mailCCAddressesID = new List<ID>();
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SPE_Pastdue_Notification'];
for (GroupMember gm : g.groupMembers) {
idList.add(gm.userOrGroupId);
} //End group
User[] usr = [SELECT email FROM user WHERE id IN :idList];
for(User u : usr) {
mailCCAddresses.add(u.email);
mailCCAddressesID.add(u.id);
} //end for loop
return mailCCAddresses;
} //End getPublicGroupAddresses
//**************************************************
public void sendMail() {
//**************************************************
//define variables
String debugAddress = 'xxx@newpagecorp.com';
String SPEEmailTemplateName = 'Opportunity_Past_Due_Notice';
String debugMessage;
String[] toAddresses;
Date CalDate = date.Today().addDays(14);
System.Debug('Date '+CalDate);
// build the Overdue list
//get the list of people with overdue Sales_Policy_Exceptions__c - this can justifiably come back empty.
List<Id> SPEIdsList = getSPEEmailAddresses(CalDate);
List<Id> SPEgroupList = getPublicGroupAddresses();
//Set the templates
EmailTemplate SPETemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :SPEEmailTemplateName];
if(SPETemplate != null && SPEIdsList.isEmpty() == false)
{
Messaging.MassEmailMessage SPEMail = new Messaging.MassEmailMessage();
SPEMail.setTargetObjectIds(SPEIdsList);
SPEMail.setTemplateId(SPETemplate.Id);
SPEMail.setUseSignature(false);
SPEMail.setSaveAsActivity(true);
// Send the email
try {
Messaging.sendEmail(new Messaging.MassEmailMessage[] { SPEMail });
System.Debug('Mail sent');
}catch(Exception e)
{
System.Debug(e);
}
}
else
{
System.Debug('SPECronJob:sendMail(): Either an email template could not be found, or no opportunities are overdue');
}//end if
}//end sendMail()
} //SPEs_Pastdue end
/**************************************************************************/
//The SPEs past due Notice Emailer
//
//This class can be scheduled to run nightly to send emails to SPE
//owners, and the public group called SPE_Pastdue_Notification
// when the End DAte (End_Date__c) is within 14 days of being past due
// and the routing status = approved.
//
/***************************************************************************/
global class SPEs_Pastdue implements Schedulable{
//mandataory function called by the Apex Scheduler
//**************************************************
global void execute(SchedulableContext SC) {
//**************************************************
sendmail(); // our main function that does the email sending
}//end execute
//Get email addresses of people pastdue SPEs by 14 days
//**************************************************
public List<Id> getSPEEmailAddresses(Date CalDate) {
//**************************************************
List<Id> mailToIds = new List<Id>();
//find a list of SPEs with overdue end dates
Sales_Policy_Exceptions__c[] c = [SELECT Id, End_Date__C, Routing_Status__c, owner_email__c
FROM Sales_Policy_Exceptions__c
WHERE End_Date__c <= : CalDate
AND Routing_Status__c = 'Approved'
];
// Old Date samples
// DAY_IN_MONTH(CloseDate) = : Day
// AND CALENDAR_MONTH(CloseDate) = : Month
// AND CALENDAR_Year(CloseDate) = : Year
//add the list of Sales_Policy_Exceptions__c to a list
for(Sales_Policy_Exceptions__c recipient : c) {
System.Debug('\n*******Found Sales_Policy_Exceptions__c Recipient');
if (recipient.owner_Email__c != '')
{
mailToIds.add(recipient.Id); // add to email SPE array
System.Debug('\n*******Recipient: '+ recipient.owner_email__c);
} else {
//Just add to chatter array
System.Debug('\n*******NO Recipient');
} //end if
} //end for loop
return mailToIds; //return the list
} //end getSPEEmailAddresses()
//**************************************************
public List<String> getPublicGroupAddresses() {
//**************************************************
List<String> idList = new List<String>();
List<String> mailCCAddresses = new List<String>();
List<ID> mailCCAddressesID = new List<ID>();
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SPE_Pastdue_Notification'];
for (GroupMember gm : g.groupMembers) {
idList.add(gm.userOrGroupId);
} //End group
User[] usr = [SELECT email FROM user WHERE id IN :idList];
for(User u : usr) {
mailCCAddresses.add(u.email);
mailCCAddressesID.add(u.id);
} //end for loop
return mailCCAddresses;
} //End getPublicGroupAddresses
//**************************************************
public void sendMail() {
//**************************************************
//define variables
String debugAddress = 'xxx@newpagecorp.com';
String SPEEmailTemplateName = 'Opportunity_Past_Due_Notice';
String debugMessage;
String[] toAddresses;
Date CalDate = date.Today().addDays(14);
System.Debug('Date '+CalDate);
// build the Overdue list
//get the list of people with overdue Sales_Policy_Exceptions__c - this can justifiably come back empty.
List<Id> SPEIdsList = getSPEEmailAddresses(CalDate);
List<Id> SPEgroupList = getPublicGroupAddresses();
//Set the templates
EmailTemplate SPETemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :SPEEmailTemplateName];
if(SPETemplate != null && SPEIdsList.isEmpty() == false)
{
Messaging.MassEmailMessage SPEMail = new Messaging.MassEmailMessage();
SPEMail.setTargetObjectIds(SPEIdsList);
SPEMail.setTemplateId(SPETemplate.Id);
SPEMail.setUseSignature(false);
SPEMail.setSaveAsActivity(true);
// Send the email
try {
Messaging.sendEmail(new Messaging.MassEmailMessage[] { SPEMail });
System.Debug('Mail sent');
}catch(Exception e)
{
System.Debug(e);
}
}
else
{
System.Debug('SPECronJob:sendMail(): Either an email template could not be found, or no opportunities are overdue');
}//end if
}//end sendMail()
} //SPEs_Pastdue end
Ok just checked the docs which confirms that setccAddresses is a Single Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm#apex_classes_outbound_single) Method. Only Base Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_base.htm#apex_classes_email_outbound_base) Methods and those methods that are specific to the Mass Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm#apex_Messaging_MassEmailMessage_methods) Class are permitted when sending bulk emails(Mass emails) via Apex.
So,you need to use setTargetObjectIs and populate the list with ccAdress list too.
All Answers
SPEMail.setTargetObjectIds(SPEIdsList);
SPEMail.ccAddresses(SPEgroupList);//Here we are setting the ccAddresses to the Group Members Email
Then you are good to go :)
If this helps,please mark it as best answer to help others :)
//**************************************************
public List<Id> getPublicGroupAddresses() {
//**************************************************
List<String> idList = new List<String>();
List<String> mailCCAddresses = new List<String>();
List<ID> mailCCAddressesID = new List<ID>();
Group g = [SELECT (select userOrGroupId from groupMembers) FROM group WHERE name = 'SPE_Pastdue_Notification'];
for (GroupMember gm : g.groupMembers) {
idList.add(gm.userOrGroupId);
} //End group
User[] usr = [SELECT id,email FROM user WHERE id IN :idList];
for(User u : usr) {
mailCCAddresses.add(u.email);
mailCCAddressesID.add(u.id);
} //end for loop
return mailCCAddressesID;
} //End getPublicGroupAddresses
//**************************************************
public void sendMail() {
//**************************************************
//define variables
String debugAddress = 'Karol.Freeberg@newpagecorp.com';
String SPEEmailTemplateName = 'Opportunity_Past_Due_Notice';
String debugMessage;
String[] toAddresses;
Date CalDate = date.Today().addDays(14);
System.Debug('Date '+CalDate);
// build the Overdue list
//get the list of people with overdue Sales_Policy_Exceptions__c - this can justifiably come back empty.
List<Id> SPEIdsList = getSPEEmailAddresses(CalDate);
List<Id> SPEgroupList = getPublicGroupAddresses();
//Set the templates
EmailTemplate SPETemplate = [select Id,Name,Subject,body from EmailTemplate where DeveloperName = :SPEEmailTemplateName];
if(SPETemplate != null && SPEIdsList.isEmpty() == false)
{
Messaging.MassEmailMessage SPEMail = new Messaging.MassEmailMessage();
SPEMail.setTemplateId(SPETemplate.Id);
SPEMail.setTargetObjectIds(SPEIdsList);
SPEMail.ccAddresses(spegrouplist);
SPEMail.setUseSignature(false);
SPEMail.setSaveAsActivity(true);
String[] ccAddresses=new String[]{'test@abc.com'};
SPEMail.setccAddresses(ccAddresses);
Ok just checked the docs which confirms that setccAddresses is a Single Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm#apex_classes_outbound_single) Method. Only Base Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_base.htm#apex_classes_email_outbound_base) Methods and those methods that are specific to the Mass Email Message (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_mass.htm#apex_Messaging_MassEmailMessage_methods) Class are permitted when sending bulk emails(Mass emails) via Apex.
So,you need to use setTargetObjectIs and populate the list with ccAdress list too.
did you managed to find any resoultion to this issue ?. In my batch i need to set the ccaddress for my massemail template