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 

write 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.
sandeep sankhlasandeep sankhla
Hi,

You can simply insert and update the Evaluation_by_metric__c record by providing necessary value so it will cover all your trigger code..

but as a best practise we should not use for inside for and SOQL inside for..which you have used and it will give you the error when it hits the limit..please optimize your code using collections..

Thanks,
Sandeep
Varshitha KVarshitha K
Can you write the code for this trigger?
 
sandeep sankhlasandeep sankhla
Hi,

can you elaborate your requirnmnet so I can guide you for  trigger code using best practise and then you can implement the same...

Thanks
Varshitha KVarshitha K
i am given just the above trigger,i need to write test class for this trigger,so that code coverage is greater than 75%
Varshitha KVarshitha K
i need to use @isTest 
akhil Vakhil V
Hi varshitha,

its simple ,insert and update the values according to the trigger scenario in the test class step by step, you can achieve or else explain the scenario then i can help
Thanks,
Akhil
akhil Vakhil V
Hi varshitha,
@isTest
private class className{
public static testmethod void methodname{
}
Evaluation_by_metric__c obj=new Evaluation_by_metric__c ();
//values
insert obj;
//updated values
update obj;
}
I hope you are beginner to write test classes just go through the google.
Thanks,
Akhil.
 
Varshitha KVarshitha K
i want complete test class code for before insert and before update trigger on above
 Evaluation_by_metric__c object.
Varshitha KVarshitha K

insert is working fine,but i dont know what to write for before update code
Varshitha KVarshitha K
Actually,my main requirement is when i create a record in evaluation object,i can select q1 or q2 from quarter field and save(let me take q1).Now,again,if i create a new record and select the same quarter(q1) as before,it should throw an error msg.similarly,in the case of updation of record,it should throw error.so,plz send me the code.
akhil Vakhil V
Hi Varshitha,
If insert is working fine.Take some fields and update it.Thier is nothing Difficult to code for before update.Once show the code what you have written?

Thanks,
Akhil.
Varshitha KVarshitha K
@isTest 

private class limitrecordtypesTest{

    
   static testMethod void lim() {
   Evaluation_by_metric__c eva = new Evaluation_by_metric__c(Quarter__c='Q1',Month__c='Jan');    
   insert eva;
   
    Close_Period__c closePeriod = new Close_Period__c(Name = 'Test Close Period');
        insert closePeriod;
        
      /*  eva = [SELECT Quarter__c,Month__c FROM Evaluation_by_metric__c WHERE Id =:eva .Id];
        
       System.assertEquals('Q2', eva .Quarter__c);
        System.assertEquals('feb',eva.Month__c);
        // eva.Quarter__c='Q2';
        // eva.Month__c='Feb';
        update eva;*/
        
       }
        
        Static List<Evaluation_by_metric__c> createEvaluation(){
    Evaluation_by_metric__c ev1=new Evaluation_by_metric__c(Quarter__c='Q2',Month__c='Feb');
     List<Evaluation_by_metric__c> evList=new List<Evaluation_by_metric__c>();
   evList.add(ev1);
    Database.Insert(evList);
   
   return evList;
        }}

this is my code.
akhil Vakhil V
Hi Varshitha,

eva. Quarter__c='updated Quarter';
eva.Month__c='Updated MOnth);   
upate eva;

closePeriod .Name='Updated Name';
update closedPeriod;

Thanks,
Akhil.

 
Varshitha KVarshitha K
but i am getting only 13% code coverage.
Varshitha KVarshitha K
how to get id and recordtypeid?
akhil Vakhil V
Hi,
For Eg:
RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='A' Limit 1];

pass values to Satisfy the if conditions that you have written in trigger so that you can get coverage.and pass all the values accorging to the trigger 

Thanks,
Akhil