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
Bhargav krishna 1Bhargav krishna 1 

How to write the test class for the trigger

Hi all,
Can anyone help me how to write the test class for the below trigger.
trigger CousnselorNotification on Inquiry_Form_Leads__c (before insert) {
     Map<String,Id> zipCodeUserMap = new Map<String,Id>();
     
      for(Zip_Code__c custom : [Select id,ZipCode__c, Counserlor__c  from Zip_Code__c ]) {
        zipCodeUserMap.put(custom.ZipCode__c,custom.Counserlor__c  );    
    }
       for(Inquiry_Form_Leads__c l : Trigger.new) {
        if(zipCodeUserMap.containsKey(l.Zip_Code__c)) {
            l.Ownerid = zipCodeUserMap.get(l.Zip_Code__c);
        }
     }
     
    Map<String,String> mapZipForEmail = new Map<String,String>();
    //Postal code from Lead object and Zip code from Zip_code__c
    for(Zip_Code__c thisZipCode : [Select id,ZipCode__c,Counserlor__c from Zip_Code__c ]){
        mapZipForEmail.put(thisZipCode.ZipCode__c, thisZipCode.Counserlor__c);
    }
 
    //EmailTemplate templateId = [Select id from EmailTemplate where name ='New Student Alert'];
  //  Create a master list to hold the emails
  List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
  
  for (Inquiry_Form_Leads__c myLead : Trigger.new) {
    if(mapZipForEmail.containsKey(myLead.Zip_Code__c)){
      
          Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
          
      //  list of people who should get the email
          List<String> sendTo = new List<String>();
          
          sendTo.add(mapZipForEmail.get(myLead.Zip_Code__c));
          mail.setToAddresses(sendTo);
        
          // Set email is sent from
          mail.setReplyTo('thredz@gmaiul.com');
          mail.setSenderDisplayName('Test');
          mail.setSaveAsActivity(false);
        
          mail.setTargetObjectId(UserInfo.getUserId());
          //mail.setTargetObjectId(Zip_Code__c.getUserId());
          // Set email contents
          //mail.setTemplateID(templateId.Id);//This is the template you are setting
        
          // Set email contents
         mail.setSubject('New Student');
          String body = 'Dear Counselor  ' + + ', ';
          body +='You Have a New Student.'+ myLead.Name+',';
          body +='Student Email.'+myLead.Email__c+',';
          body +='Student Mobile.' + myLead.Cell_Phone_Number__c;
          mail.setHtmlBody(body);
        
          // Add your email to the master list
          mails.add(mail);
      }
    
  }
  // Send all emails in the master list
  if(mails.size()>0)    Messaging.sendEmail(mails);
}

 
jasujaSFDCjasujaSFDC
Hi Bharghav, since triggers fire on dml just need to do the relevant dml operations .Try below code

@isTest
      public class ApexTest_AccountTrigger {
     static testMethod void TestAccount() {
     Zip_Code__c a = new Zip_Code__c ();
    a.Name ='Test ';
     insert a;
 
}
}