You need to sign in to do that
Don't have an account?
sundhar.mks1.3962649227519546E12
Error: List has no rows for assignment to SObject:
Hi,
I have used following code to send the Email template with attachment to two record type 1.Riser Service sheet 2.Sprinkler Riser working well, but Sprinkler when i enter the value to Email field i got ERROR: Error:Apex trigger OpportunityTriggers caused an unexpected exception, contact your administrator: OpportunityTriggers: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, List has no rows for assignment to SObject: []: Class.OpportunityTriggerHandler.EmailAttachment: line 1170, column 1
Please refer the following code
============================
Private void SendEmailAttachment(Map<Id,Opportunity> newOppMap,Map<Id,Opportunity> oldOppMap){
List<id> listMSSIds=new List<Id>(); // list of Mechanical Service Sheet Id
String templateName;
List<String> mailId;
System.Debug('--------Starting----------');
for(Opportunity opp:newOppMap.values()){ //Collecting the Mechanical Service Sheet ids from the Triggered Opportunities
if(opp.Mechanical_Service_Sheet__c != NULL){
listMSSIds.add(opp.Mechanical_Service_Sheet__c);
}
}
for(Opportunity opp:[select id,MSS_Service_Type__c,Copy_Email_for_Invoice__c,ownerId,Mechanical_Service_Sheet__c,
Accounts_Approved_Rejected__c,Management_Approved_Rejected__c,Service_Approved_Rejected__c ,Date_Copy_Invoice_Sent__c,
Priced__c , Manually_Sage_Invoiced__c,Manual_Invoicing__c,Invoice_email_to__c ,Email_for_Copy_SS_RS_SW__c
from Opportunity where id=:newOppMap.keyset()]){
if(opp.Copy_Email_for_Invoice__c!=NULL && opp.MSS_Service_Type__c!=NULL &&
(((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')
|| ((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c==NULL) ||
((opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')) &&
(opp.Copy_Email_for_Invoice__c!=oldOppMap.get(opp.id).Copy_Email_for_Invoice__c || opp.Manually_Sage_Invoiced__c!=oldOppMap.get(opp.id).Manually_Sage_Invoiced__c
||opp.Manual_Invoicing__c!=oldOppMap.get(opp.id).Manual_Invoicing__c)){
mailId = new List<String>();
templateName='Copy Invoice and MSS Cert';
mailId.add(opp.Copy_Email_for_Invoice__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,false);
}
system.debug('Opportunity==='+opp);
system.debug('Old Opp===='+oldOppMap.get(opp.id));
if(opp.Accounts_Approved_Rejected__c =='Approved' && opp.Management_Approved_Rejected__c =='Approved' &&
opp.Manually_Sage_Invoiced__c!='Yes' && opp.Service_Approved_Rejected__c =='Approved' && opp.Priced__c =='PRICED' && opp.MSS_Service_Type__c!=NULL &&
(((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No') || ((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c==NULL) || ((opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')) &&
(opp.Accounts_Approved_Rejected__c!=oldOppMap.get(opp.id).Accounts_Approved_Rejected__c || opp.Management_Approved_Rejected__c != oldOppMap.get(opp.id).Management_Approved_Rejected__c ||
opp.Service_Approved_Rejected__c!=oldOppMap.get(opp.id).Service_Approved_Rejected__c || opp.Priced__c!= oldOppMap.get(opp.id).Priced__c ||
opp.Manually_Sage_Invoiced__c!=oldOppMap.get(opp.id).Manually_Sage_Invoiced__c)){
templateName='MSS Opps Invoice';
mailId = new List<String>();
mailId.add(opp.Invoice_email_to__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,true);
}
if(opp.Mechanical_Service_Sheet__c != NULL && opp.Email_for_Copy_SS_RS_SW__c != NULL &&
(opp.Mechanical_Service_Sheet__c !=oldOppMap.get(opp.id).Mechanical_Service_Sheet__c
||opp.Email_for_Copy_SS_RS_SW__c !=oldOppMap.get(opp.id).Email_for_Copy_SS_RS_SW__c ) ){
templateName='Fire Supresion Sheet from Opp';
mailId = new List<String>();
mailId.add(opp.Email_for_Copy_SS_RS_SW__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,false);
}
}
}
Private void EmailAttachment(String templateName,List<id> listMSSIds,Opportunity opp,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(opp.MSS_Service_Type__c=='Riser Service Sheet' || opp.MSS_Service_Type__c=='Sprinkler Service Sheet' ){
attachmentList=[select name,body from Attachment Where ParentId IN:listMSSIds];
}
efaList = new List<Messaging.Emailfileattachment>();
email = new Messaging.SingleEmailMessage();
email.setTemplateId(template.id);
//email.setTargetObjectId(opp.ownerId);
email.setTargetObjectId(tempContact.id);
email.setWhatId(opp.Id);
email.setSaveAsActivity(false);
if(isOriginal){email.setCCAddresses(new list<String>{'Sundhar.mks@gmail.com'});}
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);
Messaging.sendEmail(lstMsgs);
//Delete the temp contact.
delete tempContact;
System.Debug('--------Ending----------'+lstMsgs);
}
Thanks.
I have used following code to send the Email template with attachment to two record type 1.Riser Service sheet 2.Sprinkler Riser working well, but Sprinkler when i enter the value to Email field i got ERROR: Error:Apex trigger OpportunityTriggers caused an unexpected exception, contact your administrator: OpportunityTriggers: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, List has no rows for assignment to SObject: []: Class.OpportunityTriggerHandler.EmailAttachment: line 1170, column 1
Please refer the following code
============================
Private void SendEmailAttachment(Map<Id,Opportunity> newOppMap,Map<Id,Opportunity> oldOppMap){
List<id> listMSSIds=new List<Id>(); // list of Mechanical Service Sheet Id
String templateName;
List<String> mailId;
System.Debug('--------Starting----------');
for(Opportunity opp:newOppMap.values()){ //Collecting the Mechanical Service Sheet ids from the Triggered Opportunities
if(opp.Mechanical_Service_Sheet__c != NULL){
listMSSIds.add(opp.Mechanical_Service_Sheet__c);
}
}
for(Opportunity opp:[select id,MSS_Service_Type__c,Copy_Email_for_Invoice__c,ownerId,Mechanical_Service_Sheet__c,
Accounts_Approved_Rejected__c,Management_Approved_Rejected__c,Service_Approved_Rejected__c ,Date_Copy_Invoice_Sent__c,
Priced__c , Manually_Sage_Invoiced__c,Manual_Invoicing__c,Invoice_email_to__c ,Email_for_Copy_SS_RS_SW__c
from Opportunity where id=:newOppMap.keyset()]){
if(opp.Copy_Email_for_Invoice__c!=NULL && opp.MSS_Service_Type__c!=NULL &&
(((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')
|| ((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c==NULL) ||
((opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')) &&
(opp.Copy_Email_for_Invoice__c!=oldOppMap.get(opp.id).Copy_Email_for_Invoice__c || opp.Manually_Sage_Invoiced__c!=oldOppMap.get(opp.id).Manually_Sage_Invoiced__c
||opp.Manual_Invoicing__c!=oldOppMap.get(opp.id).Manual_Invoicing__c)){
mailId = new List<String>();
templateName='Copy Invoice and MSS Cert';
mailId.add(opp.Copy_Email_for_Invoice__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,false);
}
system.debug('Opportunity==='+opp);
system.debug('Old Opp===='+oldOppMap.get(opp.id));
if(opp.Accounts_Approved_Rejected__c =='Approved' && opp.Management_Approved_Rejected__c =='Approved' &&
opp.Manually_Sage_Invoiced__c!='Yes' && opp.Service_Approved_Rejected__c =='Approved' && opp.Priced__c =='PRICED' && opp.MSS_Service_Type__c!=NULL &&
(((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No') || ((!opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c==NULL) || ((opp.Manual_Invoicing__c) && opp.Manually_Sage_Invoiced__c=='No')) &&
(opp.Accounts_Approved_Rejected__c!=oldOppMap.get(opp.id).Accounts_Approved_Rejected__c || opp.Management_Approved_Rejected__c != oldOppMap.get(opp.id).Management_Approved_Rejected__c ||
opp.Service_Approved_Rejected__c!=oldOppMap.get(opp.id).Service_Approved_Rejected__c || opp.Priced__c!= oldOppMap.get(opp.id).Priced__c ||
opp.Manually_Sage_Invoiced__c!=oldOppMap.get(opp.id).Manually_Sage_Invoiced__c)){
templateName='MSS Opps Invoice';
mailId = new List<String>();
mailId.add(opp.Invoice_email_to__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,true);
}
if(opp.Mechanical_Service_Sheet__c != NULL && opp.Email_for_Copy_SS_RS_SW__c != NULL &&
(opp.Mechanical_Service_Sheet__c !=oldOppMap.get(opp.id).Mechanical_Service_Sheet__c
||opp.Email_for_Copy_SS_RS_SW__c !=oldOppMap.get(opp.id).Email_for_Copy_SS_RS_SW__c ) ){
templateName='Fire Supresion Sheet from Opp';
mailId = new List<String>();
mailId.add(opp.Email_for_Copy_SS_RS_SW__c);
EmailAttachment(templateName,listMSSIds,opp,mailId,false);
}
}
}
Private void EmailAttachment(String templateName,List<id> listMSSIds,Opportunity opp,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(opp.MSS_Service_Type__c=='Riser Service Sheet' || opp.MSS_Service_Type__c=='Sprinkler Service Sheet' ){
attachmentList=[select name,body from Attachment Where ParentId IN:listMSSIds];
}
efaList = new List<Messaging.Emailfileattachment>();
email = new Messaging.SingleEmailMessage();
email.setTemplateId(template.id);
//email.setTargetObjectId(opp.ownerId);
email.setTargetObjectId(tempContact.id);
email.setWhatId(opp.Id);
email.setSaveAsActivity(false);
if(isOriginal){email.setCCAddresses(new list<String>{'Sundhar.mks@gmail.com'});}
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);
Messaging.sendEmail(lstMsgs);
//Delete the temp contact.
delete tempContact;
System.Debug('--------Ending----------'+lstMsgs);
}
Thanks.
Here you should use list instead of sObject directly to prevent the error.