You need to sign in to do that
Don't have an account?
shweta raghav 13
how to write test class for the below code of email and attachment
if(oppList.size()>0){
for(Opportunity opp : oppList){
if(opp.StageName== '100% - Closed Won'){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] {'sh@gmail.com'};
message.optOutPolicy = 'FILTER';
message.setSubject(' New attachment on Opportunity : ' + ' ' + opp .name+' ' +'with probability 100% for Account'+' ' + opp.Account.Name);
message.setHtmlBody ('Hi Team, <br/><br/> There is a new attachment for opportunity '+opp .name+' Please review the attached document<br/><br/><a href="https://c3.salesforce.com/'+opp.id+'">https://c3.salesforce.com/'+opp.id+'</a><br/><br/>Regards<br/><br/>Salesforce Admin');
for(Attachment a : trigger.new){
if(a.parentId == opp.id){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
efa.setContentType(a.ContentType);
fileAttachments.add(efa);
}
}
if(!fileAttachments.isEmpty()){
Message.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
}
for(Opportunity opp : oppList){
if(opp.StageName== '100% - Closed Won'){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] {'sh@gmail.com'};
message.optOutPolicy = 'FILTER';
message.setSubject(' New attachment on Opportunity : ' + ' ' + opp .name+' ' +'with probability 100% for Account'+' ' + opp.Account.Name);
message.setHtmlBody ('Hi Team, <br/><br/> There is a new attachment for opportunity '+opp .name+' Please review the attached document<br/><br/><a href="https://c3.salesforce.com/'+opp.id+'">https://c3.salesforce.com/'+opp.id+'</a><br/><br/>Regards<br/><br/>Salesforce Admin');
for(Attachment a : trigger.new){
if(a.parentId == opp.id){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
efa.setContentType(a.ContentType);
fileAttachments.add(efa);
}
}
if(!fileAttachments.isEmpty()){
Message.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
}
Try below code snippet
Can you share the whole code.
if(trigger.IsInsert) {
if(trigger.IsBefore){
set<id> parentIds = New set<id>();
List <String> attachName = new List<string>();
for(attachment att : trigger.new){
if (att.parentId !=null){
system.debug('att.parentId==='+att.parentId);
parentIds.add(att.parentId);
system.debug('att.attachName==='+att.Name);
attachName.add(att.name);
}
}
if(parentIds!=null){
system.debug('parentIds---'+attachName+'parentIds --- sec----'+parentIds);
List<attachment> attachlist = new List<attachment>();
attachlist=[SELECT Id, name FROM Attachment WHERE ParentId In : parentIds AND Name =: attachName];
system.debug('attachlist()--'+attachlist);
if(!attachlist.isEmpty()){
delete attachlist;
}
}
}if(trigger.IsAfter){
set<id> parentIds = New set<id>();
for(attachment att : trigger.new){
String parentIdString = String.valueof(att.parentId);
if (att.parentId !=null && parentIdString.substring(0,3) == '006'){
parentIds.add(att.parentId);
}
}
List<Opportunity> oppList = New list <Opportunity>([SELECT Id,StageName,Name,Account.Name FROM Opportunity WHERE Id IN : parentIds]);
List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
if(oppList.size()>0){
for(Opportunity opp : oppList){
if(opp.StageName== '100% - Closed Won'){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] {'s@gmail.com'};
message.optOutPolicy = 'FILTER';
message.setSubject(' New attachment on Opportunity : ' + ' ' + opp .name+' ' +'with probability 100% for Account'+' ' + opp.Account.Name);
message.setHtmlBody ('Hi Team, <br/><br/> There is a new attachment for opportunity '+opp .name+' .Please review the attached document<br/><br/><a href="https://c3.salesforce.com/'+opp.id+'">https://c3.salesforce.com/'+opp.id+'</a><br/><br/>Regards<br/>Salesforce Admin');
for(Attachment a : trigger.new){
if(a.parentId == opp.id){
Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
efa.setContentType(a.ContentType);
fileAttachments.add(efa);
}
}
if(!fileAttachments.isEmpty()){
Message.setFileAttachments(fileAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message });
}
}
}
}
}
}
}
You are first deleting attachments and then getting opportunities using attachment parentId.
By the way, try the below code. Test class seems fine, the issue can be with your trigger logic.
So first trigger deletes all the attachments and then send emails.