You need to sign in to do that
Don't have an account?
Terry Wyman
Test class at 66% coverage in sandbox but 0% in production
I am trying to deploy a simple invocablemethod class to send emails out when a lead is added to a campaign. I had used hard coded IDs which provided 100% coverage in the sandbox but 0% in production, I've removed the hardcoded references. Any help is appreciated, my code and test class are below:
I am hoping that it is something simple that I've missed.
public class SendEmailToLead { @InvocableMethod public static void sendEmail(List<CampaignMember> CampaignMembers) { List<CampaignMember> thecampaignmembers = new List<Campaignmember>(); for (CampaignMember cm : CampaignMembers) { List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(cm.LeadId); List<String> sendTo = new List<String>(); sendTo.add(cm.Email); mail.setToAddresses(sendTo); mail.setReplyTo('nomail@nomail.com'); mail.setSenderDisplayName('nomail'); Campaign c = [SELECT Id FROM Campaign WHERE Name = 'some name' LIMIT 1]; if(cm.CampaignId == c.Id) { EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'some name' LIMIT 1]; mail.setTemplateId(et.id); List<String> bccTo = new List<String>(); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); mail.setBccAddresses(BccTo); } Campaign c2 = [SELECT Id FROM Campaign WHERE Name = 'some name' LIMIT 1]; if(cm.CampaignId == c2.Id) { EmailTemplate et2=[Select id from EmailTemplate where DeveloperName=:'some name' LIMIT 1]; mail.setTemplateId(et2.id); List<String> bccTo = new List<String>(); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); bccTo.add('nomail@nomail.com'); mail.setBccAddresses(BccTo); } mails.add(mail); Messaging.sendEmail(mails); } } }and the test class is below:
@isTest // Tests the SendEmailToLead Trigger public class TestSendEmailToCampaignMember { static testMethod void ValidateSendEmailToCampaignMember() { Lead l = new Lead(); l.FirstName = 'Testing'; l.LastName = 'Lead'; l.Company = 'Test Company'; l.Industry = 'Banking'; l.LeadSource = 'some source'; l.Rating = 'Normal'; l.Email = 'test@nomail.com'; l.Street = '123 Main St'; l.State = 'FL'; l.PostalCode = '34202'; l.Lead_Date__c = date.today(); l.HasOptedOutOfEmail = False; l.Internal_Comments__c = 'x'; l.Project_Details__c = 'x'; insert l; Campaign c = new Campaign(); c.Name = 'some name'; c.Status = 'Planning'; c.Type = 'Seminar/Conference'; c.StartDate = date.today(); c.EndDate = date.today(); c.OwnerId = UserInfo.getUserId(); insert c; CampaignMember cm = new CampaignMember(); cm.CampaignId = c.Id; cm.LeadId = l.Id; cm.Status = 'Sent'; insert cm; Lead l2 = new Lead(); l2.FirstName = 'Testing2'; l2.LastName = 'Lead2'; l2.Company = 'Test Company2'; l2.Industry = 'Banking'; l2.LeadSource = 'some source'; l2.Rating = 'Normal'; l2.Email = 'test@nomail.com'; l2.Street = '123 Main St'; l2.State = 'FL'; l2.PostalCode = '34202'; l2.Lead_Date__c = date.today(); l2.HasOptedOutOfEmail = False; l2.Internal_Comments__c = 'x'; l2.Project_Details__c = 'x'; insert l2; Campaign c2 = new Campaign(); c.Name = 'some name'; c.Status = 'Planning'; c.Type = 'Seminar/Conference'; c.StartDate = date.today(); c.EndDate = date.today(); c.OwnerId = UserInfo.getUserId(); insert c2; CampaignMember cm2 = new CampaignMember(); cm2.LeadId = l2.Id; cm2.CampaignId = c2.Id; cm2.Status = 'Sent'; insert cm2; } }
I am hoping that it is something simple that I've missed.
Try this,
Hope this will work :)
Let me know if you need more help.
Please mark the question solved by selecting a best answer if it soves the issue.
Good luck :)
Try This:
Apex class:
TestClass:
Hope this helps you!
If this helps you, please mark it as solved so that it will be available for others as a proper solution.
Thanks
Adarsh
Ok, I have given A look on your code of sending Email. That does not seem to perfectly alright ( I might be wrong about that).
What i Belive is SetTargetobjectid() and setTempateid() id used together to link a target object with specified template. and there is no need to specify sendTo , as the email will be sent to the email of the Targetobject.
While sendto, setreplyTo() these methods are used when you define email body in code.
Are you sure, your Code sends email to the right person with right template. Becuase if test case showing this error. there must be something wrong with the actual class.
Regards
--Abhilash
you are right regarding the sendTo variable, it is included in the template...I took that line out...The difficult part is, it executes just fine...
do you have email templates that have Developer name some name ?
Thank you to everyone who has helped thus far.