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

Messaging.SingleEmailMessage Test Code
This is my code, I am sending email to Installers, I need a test code for this. I wonder if anyone can help?
Thanks in advance.
trigger handleServiceEmail on Case (after update) { Set<Id> caseIds = new Set<Id>(); List<Case> cl = [SELECT id, Service__r.Installer__c, Case_Type__c FROM Case WHERE id IN: trigger.newMap.keyset()]; Id[] targetCaseIds = new Id[] {}; List<Messaging.SingleEmailMessage> email = new List<Messaging.SingleEmailMessage>(); for(Case c : cl){ if(c.Case_Type__c == 'Urgent') { caseIds.add(c.Service__r.Installer__c); targetCaseIds.add(c.id); OrgWideEmailAddress owa = [select id from OrgWideEmailAddress]; EmailTemplate template = [SELECT id FROM EmailTemplate WHERE DeveloperName = 'Urgent Email' LIMIT 1]; for(Contact contact :[SELECT id,Email FROM Contact WHERE AccountID IN: caseIds]){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setOrgWideEmailAddressId(owa.Id); mail.setTargetObjectId(contact.Id); mail.setTemplateID(template.ID); mail.setSaveAsActivity(false); mail.setWhatId(c.ID); email.add(mail); } } } Messaging.sendEmail(email); }
Test method is used to test execution of your apex script, since you have written the trigger that fires on update of case so you need to create a case and then update it.
Sample code for your trigger is given below:
// there is some basic idea you need to fill it with meaning full data.
public static testMethod void myTestMethod()
{
Installer__c installer=new Installer__c(give field's values.........);
insert installer;
Service__c service=new Service__c(Installer__c=installer.id,.......give other field's value....);
insert service;
Case c1=new Case(Service__c=service.id,....give other field's value.....);
insert c1;
Case c2=[select id,Case_Type__c from case where id=:c1.id];
c2.Case_Type__c='Urgent';
update c2;
}
Hi,
Hi,
have you covered the Email section, Even im facing same issue to cover the
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emails);
mail.setReplyTo('no-reply@mas.com');
.........
....
Messaging.EmailFileAttachment efaPage = new Messaging.EmailFileAttachment();
.....