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
Varshitha KVarshitha K 

writing test class for a trigger

I have the following trigger.
 
trigger limitrecordtypes on Evaluation_by_metric__c (before insert,before update) {


if(trigger.isinsert){


list<Evaluation_by_metric__c> recordset=new list<Evaluation_by_metric__c>([select id,recordtypeid,Quarter__c,Metric__c,Month__c from Evaluation_by_metric__c]);


 for(Evaluation_by_metric__c c: trigger.new)
 {
   //mapping close period record with evaluation record before insert
   c.Close_Period__c = [select id from Close_Period__c limit 1].id;
   system.debug('look '+c.Close_Period__c);
   
   if(c.Quarter__c != null)
   {
    for(Evaluation_by_metric__c cv: recordset)
       {
        if(cv.Quarter__c == c.Quarter__c && cv.recordtypeid == c.recordtypeid && cv.Metric__c==c.Metric__c)
         {
          c.Quarter__c.adderror('This Recordtype has been created for this Quarter');
         }
   }
 }
  if(c.Month__c != null)
      {
        for(Evaluation_by_metric__c ebm : recordset)
         {
            if(ebm.Month__c == c.Month__c && ebm.recordtypeid == c.recordtypeid && ebm.Metric__c==c.Metric__c)
              {
            c.Month__c.adderror('This Recordtype has been created for this Month');
              }
         }
      }
  }
 
 }
 
 
if(trigger.isupdate)
{
  list<Evaluation_by_metric__c> recordset1=new list<Evaluation_by_metric__c>([select id,recordtypeid,Quarter__c,Metric__c,Month__c from Evaluation_by_metric__c]);


 for(Evaluation_by_metric__c c: trigger.new){
 
    //mapping close period record with evaluation record before insert
   c.Close_Period__c = [select id from Close_Period__c limit 1].id;
   
 Evaluation_by_metric__c oldAsset=trigger.oldMap.get(c.id);
 system.debug('@@@@@@'+oldAsset);
 
     if(oldAsset.Quarter__c !=null){
 
         for(Evaluation_by_metric__c cv: recordset1){
 
             if(cv.Quarter__c == oldAsset.Quarter__c && cv.recordtypeid == oldAsset.recordtypeid && cv.Metric__c==oldAsset.Metric__c){
             
               cv.quarter__c=oldAsset.Quarter__c;
               cv.recordtypeid = oldAsset.recordtypeid;
               cv.Metric__c=oldAsset.Metric__c;
              
               
                 
 }
 else if(cv.Quarter__c == c.Quarter__c && cv.recordtypeid == c.recordtypeid && cv.Metric__c==c.Metric__c){
 
                 c.Quarter__c.adderror('This Recordtype has been created for this Quarter');
 
 }
 }
 
 
 }
 
 
 
 if(oldAsset.Month__c != null){
 for(Evaluation_by_metric__c ebm: recordset1){
 if(ebm.Month__c == oldAsset.Month__c && ebm.recordtypeid == oldAsset.recordtypeid && ebm.Metric__c==oldAsset.Metric__c){
               ebm.Month__c =oldAsset.Month__c;
               ebm.recordtypeid = oldAsset.recordtypeid;
               ebm.Metric__c=oldAsset.Metric__c;
}
 else if(ebm.Month__c == c.Month__c && ebm.recordtypeid == c.recordtypeid && ebm.Metric__c==c.Metric__c){
 
                 c.Month__c.adderror('This Recordtype has been created for this Quarter');
 
 }
}
}
}










}
}

Can you plzz write the test class code for this trigger?
Thanks in advance.
SonamSonam (Salesforce Developers) 
Hi, Would help if you could share your test code and the community can then help you where you need help exactly - if you haven't started writing code- i'd suggest you to go through the following:
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

You need to start with creating a test record for 
Evaluation_by_metric__c object and set values such that the IF conditions you have written are tested using 
System.assertEquals if the values you want to set are being set correctly by the trigger.

 
David Holland 6David Holland 6
I would also suggest rewriting that trigger to take your select statements outside of a loop, otherwise you are likely to hit limits if you try to bulkload data etc.