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
babloo123babloo123 

Test Class on Trigger Class

Need some guidance on Test Class as the code coverage not happening properly.

Trigger and Class below.

Trigger

----------------------------------------------
trigger SendEmailtoMRO on COI_Expertise__c (after update) {
    string text1='Unclear if COI Exists';
for(COI_Expertise__c ci:trigger.new){
         
  if (ci.Conflict_of_Interest__c.equals(text1)){
 SendEmailHandler.sendEmail(ci.test__c);
  }
      }
        }  Here test__c is ID as we are using the template using ID
--------------------------------------------------------------------------------------------------------------
Class

public class SendEmailHandler {
    
    public static void sendEmail(id targetid){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 mail.setTargetObjectId(targetid);
  mail.setSenderDisplayName('PCORI');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);
    //checking condition
 
 
   //Template to select from the list of email templates available
          EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Unclear_if_COI_Exists'];
     system.debug('The ETID is' + et);
          mail.setTemplateId(et.id);
          Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   
     system.debug(r);
        
    }

}

---------------------------------------------------------------------------------------------

I get a error saying target is empty

Can someone help in writing a test case for this.
 
jp1234jp1234
Can you share your test code?
jp1234jp1234
Also, don't you need to set whatID in your email template, or does your email template just ignore the Sobject it is associated with?
SFDCpassionSFDCpassion
Am sorry , but why have you put send mail in loop in your main code? Also there is Query that is indirectly bieng called in loop. You may want to fix those before you start with test class
SFDCpassionSFDCpassion
Also in you trigger ensure ci.test__c is not null before you do " SendEmailHandler.sendEmail(ci.test__c);" assuming its lookup and not Master detail relation.