+ Start a Discussion
kiran k 12kiran k 12 

How to cover the formulae field in test class?

I have written a test class for trigger.the formulae fields is there in if condition.the problem is "Opsconca__c "formulae field in Opportunity_car_set__c.the Opsconca__c  = Opsconca__c   = (Model__r.Name) & (Rfleet_Country_Code__c) & TEXT(Opportunity__r.Total_Fleet_Size__c) ,the Rfleet_Country_Code__c is formulae field in Account object,the Total_Fleet_Size__c is formulae field in opportunity object related to account formulae field.how to cover the formulae field in IF condition of test class?
 
Test class:
@isTest
public class Rfleet_CheckDiscount_Test {
    static testMethod void Discountceck(){
        Account acc = new Account(Name='something',Montant__c=0.2);
       acc.Fleet_Size_Cars__c = 10;
        acc.Fleet_Size_LCV__c = 10;
        acc.RecordTypeId = '012m00000000QSU';
        insert acc;
        acc.Name = 'something2';
        update acc;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'restful';
        opp.StageName = 'Draft';
        opp.CloseDate = system.today();
        opp.AccountId = acc.Id;
        //opp.Total_Fleet_Size__c = 20;
        insert opp;
        opp.Name = 'restfulll';
        update opp;
       
        Product2 pro = new Product2();
        pro.Name = 'Kiran prod1';
        pro.Country_code__c = 'DZ';
        pro.Brand__c = 'Dacia';
        pro.IsActive = true;
        pro.Family = 'X44';
        insert pro;
        
        Opportunity_car_set__c ocs = new Opportunity_car_set__c();
        ocs.Name = 'Discount Test12';
        ocs.Model__c = pro.id;
        //ocs.Rfleet_Country_Code__c ='DZ';
        ocs.Opportunity__c = opp.id;
        ocs.Discount__c = 11;
        ocs.Quantity__c = 4;
        insert ocs;
        Discount_Grid__c dgc = new Discount_Grid__c();
        dgc.Rfleet_Country_Code__c = 'DZ';
        dgc.Rfleet_AOC_Model_Code__c = pro.id;
        dgc.Rfleet_Threshold_Max__c = 10;
        dgc.Rfleet_Discount__c = 10;
        insert dgc;
      Rfleet_CheckDiscount.CheckDiscountobject();
   
    }
    static testMethod void Discountceck1(){
        Account acc = new Account(Name='something',Montant__c=0.2);
       acc.Fleet_Size_Cars__c = 10;
        acc.Fleet_Size_LCV__c = 10;
        acc.RecordTypeId = '012m00000000QSU';
        insert acc;
        acc.Name = 'something2';
        update acc;
        
        Opportunity opp = new Opportunity();
        opp.Name = 'restful';
        opp.StageName = 'Draft';
        opp.CloseDate = system.today();
        opp.AccountId = acc.Id;
        //opp.Total_Fleet_Size__c = 20;
        insert opp;
        opp.Name = 'restfulll';
        update opp;
       
        Product2 pro = new Product2();
        pro.Name = 'Kiran prod1';
        pro.Country_code__c = 'DZ';
        pro.Brand__c = 'Dacia';
        pro.IsActive = true;
        pro.Family = 'X44';
        insert pro;
        
        Opportunity_car_set__c ocs = new Opportunity_car_set__c();
        ocs.Name = 'Discount Test12';
        ocs.Model__c = pro.id;
        //ocs.Rfleet_Country_Code__c ='DZ';
        ocs.Opportunity__c = opp.id;
        ocs.Discount__c = 10;
        ocs.Quantity__c = 4;
        insert ocs;
        Discount_Grid__c dgc = new Discount_Grid__c();
        dgc.Rfleet_Country_Code__c = 'DZ';
        dgc.Rfleet_AOC_Model_Code__c = pro.id;
        dgc.Rfleet_Threshold_Max__c = 10;
        dgc.Rfleet_Discount__c = 20;
        insert dgc;
      Rfleet_CheckDiscount.CheckDiscountobject();
        
        
    }
 
}

User-added image
Trigger:
global with sharing class Rfleet_CheckDiscount{
 
 webservice static void CheckDiscountobject()  { 
 
 list<Opportunity_car_set__c>lsupdateKo=new list<Opportunity_car_set__c>();
  list<Opportunity>lsupdatoppko=new list<Opportunity>();
   
  list<Discount_Grid__c> listDisGrid=[select Rfleet_Status__c,Rfleet_Discountconca__c,Rfleet_Discount__c from Discount_Grid__c  where SystemModstamp =LAST_N_DAYS:31 and Rfleet_Status__c = 'Enabled' Limit 50000];
    system.debug('Discount_Grid list:::'+listDisGrid);
  list<Opportunity> lsoppty=[Select StageName,name,Rfleet_Granted_discount_KO__c,RFLEET_Status__c, Id,(Select id,Opsconca__c,Rfleet_Granted_discount_KO__c,Discount__c From Opportunity_car_sets__r) From Opportunity where StageName IN('Draft','Internal Approbation')order by SystemModstamp desc limit 100];
               
system.debug('Opportunity list:::'+lsoppty);
           
             for(Opportunity opp:lsoppty){
             
                 for(Opportunity_car_set__c ocs:opp.Opportunity_car_sets__r){
                 
                       for(Discount_Grid__c disGrid:listDisGrid){
                   system.debug('dis con:::'+disGrid.Rfleet_Discountconca__c);
                   system.debug('Opportunity OCS con:::'+ocs.Opsconca__c);
                           if(disGrid.Rfleet_Discountconca__c==ocs.Opsconca__c)  {
                           system.debug(' OCS con:::'+ocs.Discount__c);
                           system.debug('Discount grid con:::'+disGrid.Rfleet_Discount__c);
                                  if(ocs.Discount__c>disGrid.Rfleet_Discount__c){
                                    ocs.Rfleet_Granted_discount_KO__c = true;
                                     opp.Rfleet_Granted_discount_KO__c = true;
                                    lsupdateKo.add(ocs);
                                    lsupdatoppko.add(opp);
                                    }else{
                                     ocs.Rfleet_Granted_discount_KO__c = false;
                                      opp.Rfleet_Granted_discount_KO__c = false;
                                    lsupdateKo.add(ocs);
                                    lsupdatoppko.add(opp);
                                    }
                               
                               }
                       
                       }
                   }
               }
               
         update lsupdateKo;
         update lsupdatoppko;
   }
   
}

 
mayurmayur
Hi Kiran,

No Need to cover Formula field, it will autometically cover, please use debug and check may be in output of both formula field having any ' '(space), or not inserting data which meet that criteria, or make same vaalue in both formula fields. please check formula of both formula fields, accrding to that fill data in object, then it will be automatically cover.

and my advice, change your code, your code not in best practice,  if there has thousand of records, automatically it will give "CPU ime Limit Error".