You need to sign in to do that
Don't have an account?
Varshitha K
write test class for a trigger
I have the following 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.
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
can you elaborate your requirnmnet so I can guide you for trigger code using best practise and then you can implement the same...
Thanks
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
@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.
Evaluation_by_metric__c object.
insert is working fine,but i dont know what to write for before update code
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.
this is my code.
eva. Quarter__c='updated Quarter';
eva.Month__c='Updated MOnth);
upate eva;
closePeriod .Name='Updated Name';
update closedPeriod;
Thanks,
Akhil.
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