You need to sign in to do that
Don't have an account?
Varshitha K
test class to check duplicate records
this is my trigger
and i got only 13% test coverage for test class
so,plz help me with the code.
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'); } } } } } }
and i got only 13% test coverage for test class
@isTest private class limitrecordtypesTest{ static testMethod void lim() { Boolean result = false; recordtype rec=new recordtype(); rec.name='Financial evaluation'; Metric__c met1=new Metric__c(Name='a'); Metric__c met2=new Metric__c(Name='b'); Evaluation_by_metric__c eva1 = new Evaluation_by_metric__c(Metric__c=met1.id,Quarter__c='Q1',recordtype=rec); insert eva1; Evaluation_by_metric__c eva3 = new Evaluation_by_metric__c(Metric__c=met2.id, Month__c='jan',recordtype=rec); insert eva3; Close_Period__c closePeriod1 = new Close_Period__c(id=eva1.id,Name = 'Test1 Close Period'); Close_Period__c closePeriod2 = new Close_Period__c(id=eva3.id,Name = 'Test2 Close Period'); insert closePeriod1; insert closePeriod2; try{ Evaluation_by_metric__c eva2 = new Evaluation_by_metric__c(Quarter__c='Q1',recordtype=rec); insert eva2; }catch(DmlException ex){ result = true;} System.assert(result); try{ Evaluation_by_metric__c eva4 = new Evaluation_by_metric__c(Month__c='jan',recordtype=rec); insert eva4; }catch(DmlException ex){ result = true;} System.assert(result); } }
so,plz help me with the code.
You have used two events for triggers i.e insert and update. In user test class you are not updating any records. i.e after line 41 add below lines which will help you to increate % of code cv.
eva4.Month__c='feb';
update eva4;
Also plz post screen shot of you code coverage for further help on this.
1. In your actual trigger you are querying all Evaluation_by_Metric__c records, you will want to create sets based on the quarter and month and only query this when validating. As your number of records grow, you could face Limits in the execution of this trigger.
2. You may want to consider pushing these into separate methods that would be specific to each scenario. MonthInsert, MonthUpdate, QuarterInsert, QuarterUpdate. You will also want to add tests to confirm that there are no false positives when creating records.
3. As a best practice, you will want to utilize Test.startTest and Test.stopTest.
4. Inside of the trigger.new loop, you are performing a SOQL query, you will also want to move this outside of the for loop as it will also cause issues with limits as your organization grows.
This is an example that could get your code coverage up(keep in mind I am typing this in the answer window and not validating the code through an IDE).