function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
OnurKOnurK 

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);
  
}

 

 

Pradeep_NavatarPradeep_Navatar

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;

}

SMasterSMaster

Hi,

 

I need your help regarding an issue that is related to send an email with selected attachment from the visualforce page...... i have a visualforce page that displayes Attachment of a custom object....
i have also used the inputcheckbox along with each record to select the attachment.... the need is to click the checkbox to select which file the user want to send as an attachment in the mail....... and then click send button.....
I have tried implementing this using the wrapper class.... but unable to do so... please suggest if you need.. i can also provide you with the code.. i have used so far...
SF DEVSF DEV

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();

.....