You need to sign in to do that
Don't have an account?

writing test class for a trigger
I have the following trigger.
Can you plzz write the test class code for this trigger?
Thanks in advance.
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.
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.