You need to sign in to do that
Don't have an account?
Sankar Ganesh
Can't send mail o same mail address more than once
Here I send email to email address given in field.when I give different mail id in each field its working but If I send give same email address on every field the attachment is send one time instead of multiple time.Why?
Private void SendEmailAttachment(Map<id,Mechanical_Service_Sheet__c> NewMSSMap,Map<id,Mechanical_Service_Sheet__c> oldMSSMap){
//Variable Declaration
List<id> listMSSIds=new List<Id>();
String templateName;
List<String> mailId;
System.Debug('--------Starting----------');
// list of Mechanical Service Sheet Id
for(Mechanical_Service_Sheet__c ms:NewMSSMap.values()){
listMSSIds.add(ms.id);
}
List<Mechanical_Service_Sheet__c> MSSList=[SELECT id,Name,Copy_Email_for_Sheet__c,Email_Confirmation_to__c,Email_for_HQ_Notification__c,Engineer_s_Email_Address__c,RecordType.Name,Fire_Extinguisher_Service_complete__c
FROM Mechanical_Service_Sheet__c WHERE ID=:listMSSIds];
for(Mechanical_Service_Sheet__c ms:MSSList){
if(Test.isrunningtest()||(ms.Email_Confirmation_to__c!=NULL && ms.RecordType.Name=='Sprinkler Service Sheet' && ms.Fire_Extinguisher_Service_complete__c==true &&
ms.Fire_Extinguisher_Service_complete__c!=oldMSSMap.get(ms.id).Fire_Extinguisher_Service_complete__c)){
mailId = new List<String>();
templateName='Sprinkler Confirmation';
mailId.add(ms.Email_Confirmation_to__c);
System.Debug('--------SSSmailId----------'+mailId);
EmailAttachment(templateName,listMSSIds,ms,mailId,false);
}
}
}
Private void EmailAttachment(String templateName,List<id> listMSSIds,Mechanical_Service_Sheet__c Mss,List<String> toAddress, Boolean isOriginal ){
// get The Email Template Id
EmailTemplate template = [Select id,name,subject from EmailTemplate
where name =:templateName];
//Added the below code to prevent the mail to owner
Contact tempContact = new Contact();
tempContact.LastName ='TestLast';
tempContact.firstName = 'Testname';
tempContact.email = toAddress[0];
tempContact.Account = [select id, name from Account limit 1];
Insert tempContact;
//get The List Of Attachments In Mechanical Service Sheet
List<Attachment> attachmentList=new List<Attachment>();
if(Mss.RecordType.Name=='Sprinkler Service Sheet' || Mss.RecordType.Name=='Riser Service Sheet'|| Mss.RecordType.Name=='Suppression System And Room Integrity Test'|| Mss.RecordType.Name=='Hydrant Service Sheet'|| Mss.RecordType.Name=='Fire Suppression Service Sheet'){
attachmentList=[select name,body from Attachment Where ParentId IN:listMSSIds];
}
efaList = new List<Messaging.Emailfileattachment>();
email = new Messaging.SingleEmailMessage();
if(Mss.Email_for_HQ_Notification__c!=Null){
String[] toAddresses=new String[]{Mss.Email_for_HQ_Notification__c};
email.setToAddresses(toAddresses);
}
if(Mss.Copy_Email_for_Sheet__c!=Null){
String[] toAddresses=new String[]{Mss.Copy_Email_for_Sheet__c};
email.setToAddresses(toAddresses);
}
if(Mss.Engineer_s_Email_Address__c!=Null){
String[] bccAddresses=new String[]{Mss.Engineer_s_Email_Address__c};
email.setBccAddresses(bccAddresses);
}
email.setTemplateId(template.id);
email.setTargetObjectId(tempContact.id);
email.setWhatId(Mss.Id);
email.setSaveAsActivity(false);
System.Debug('--------attachmentList----------'+attachmentList);
for(Attachment a:attachmentList){
efa= new Messaging.Emailfileattachment();
efa.setFileName(a.name);
efa.setBody(a.body);
efaList.add(efa);
}
email.setFileAttachments(efaList);
List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
lstMsgs.add(email);
if(!Test.isRunningTest()){
Messaging.sendEmail(lstMsgs);
}
//Delete the temp contact.
delete tempContact;
System.Debug('--------Ending----------'+lstMsgs);
}
Private void SendEmailAttachment(Map<id,Mechanical_Service_Sheet__c> NewMSSMap,Map<id,Mechanical_Service_Sheet__c> oldMSSMap){
//Variable Declaration
List<id> listMSSIds=new List<Id>();
String templateName;
List<String> mailId;
System.Debug('--------Starting----------');
// list of Mechanical Service Sheet Id
for(Mechanical_Service_Sheet__c ms:NewMSSMap.values()){
listMSSIds.add(ms.id);
}
List<Mechanical_Service_Sheet__c> MSSList=[SELECT id,Name,Copy_Email_for_Sheet__c,Email_Confirmation_to__c,Email_for_HQ_Notification__c,Engineer_s_Email_Address__c,RecordType.Name,Fire_Extinguisher_Service_complete__c
FROM Mechanical_Service_Sheet__c WHERE ID=:listMSSIds];
for(Mechanical_Service_Sheet__c ms:MSSList){
if(Test.isrunningtest()||(ms.Email_Confirmation_to__c!=NULL && ms.RecordType.Name=='Sprinkler Service Sheet' && ms.Fire_Extinguisher_Service_complete__c==true &&
ms.Fire_Extinguisher_Service_complete__c!=oldMSSMap.get(ms.id).Fire_Extinguisher_Service_complete__c)){
mailId = new List<String>();
templateName='Sprinkler Confirmation';
mailId.add(ms.Email_Confirmation_to__c);
System.Debug('--------SSSmailId----------'+mailId);
EmailAttachment(templateName,listMSSIds,ms,mailId,false);
}
}
}
Private void EmailAttachment(String templateName,List<id> listMSSIds,Mechanical_Service_Sheet__c Mss,List<String> toAddress, Boolean isOriginal ){
// get The Email Template Id
EmailTemplate template = [Select id,name,subject from EmailTemplate
where name =:templateName];
//Added the below code to prevent the mail to owner
Contact tempContact = new Contact();
tempContact.LastName ='TestLast';
tempContact.firstName = 'Testname';
tempContact.email = toAddress[0];
tempContact.Account = [select id, name from Account limit 1];
Insert tempContact;
//get The List Of Attachments In Mechanical Service Sheet
List<Attachment> attachmentList=new List<Attachment>();
if(Mss.RecordType.Name=='Sprinkler Service Sheet' || Mss.RecordType.Name=='Riser Service Sheet'|| Mss.RecordType.Name=='Suppression System And Room Integrity Test'|| Mss.RecordType.Name=='Hydrant Service Sheet'|| Mss.RecordType.Name=='Fire Suppression Service Sheet'){
attachmentList=[select name,body from Attachment Where ParentId IN:listMSSIds];
}
efaList = new List<Messaging.Emailfileattachment>();
email = new Messaging.SingleEmailMessage();
if(Mss.Email_for_HQ_Notification__c!=Null){
String[] toAddresses=new String[]{Mss.Email_for_HQ_Notification__c};
email.setToAddresses(toAddresses);
}
if(Mss.Copy_Email_for_Sheet__c!=Null){
String[] toAddresses=new String[]{Mss.Copy_Email_for_Sheet__c};
email.setToAddresses(toAddresses);
}
if(Mss.Engineer_s_Email_Address__c!=Null){
String[] bccAddresses=new String[]{Mss.Engineer_s_Email_Address__c};
email.setBccAddresses(bccAddresses);
}
email.setTemplateId(template.id);
email.setTargetObjectId(tempContact.id);
email.setWhatId(Mss.Id);
email.setSaveAsActivity(false);
System.Debug('--------attachmentList----------'+attachmentList);
for(Attachment a:attachmentList){
efa= new Messaging.Emailfileattachment();
efa.setFileName(a.name);
efa.setBody(a.body);
efaList.add(efa);
}
email.setFileAttachments(efaList);
List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
lstMsgs.add(email);
if(!Test.isRunningTest()){
Messaging.sendEmail(lstMsgs);
}
//Delete the temp contact.
delete tempContact;
System.Debug('--------Ending----------'+lstMsgs);
}
Please elaborate your requirement that why you want to send one email to the same address multiple times?
This will help us in easy troubleshooting the problem.
Regards,
Nagendra.P
Can we send this email template in workflow with notes and attachment if so kindly give suggestion.this is my code its work for images but if user upload any pdf its not showing
<messaging:attachment filename="{!relatedTo.Name} - Images" renderAs="pdf" >
<apex:variable value="{!0}" var="i"/>
<apex:repeat value="{!relatedTo.NotesAndAttachments}" var="c" >
<h3>Image Name : {!c.title}</h3><apex:outputText />
<h3>Date : <apex:outputText value="{0,date,dd/MM/yyyy}"><apex:param value="{!c.createddate}" /></apex:outputText></h3><br/>
<apex:image url="/servlet/servlet.FileDownload?file={!c.Id}" height="200px" width="200px"/><br/><hr/>
<apex:variable value="{!1}" var="i"/>
</apex:repeat>
<apex:outputText rendered="{!i == 0}">Images are not available for this sheet.</apex:outputText>
</messaging:attachment>