+ Start a Discussion
GMASJGMASJ 

How to write test class for apex class and trigger

Hi, 

  I wrote a trigger which called apex class need a suggestion how to write the logic to get code coverage for test class. 

Apex Class
public with sharing class ApprovalUtils {

  /* Subscription Discount Level 1 */
  public static Integer SubLevel1(Decimal Subscription, Decimal ACV, Decimal Discount) {
    
   Integer L1Count;  
    
   List<AggregateResult> Level1 = [select count(id) Level1Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Subscription' and 
              Subscription_Term__c = :Subscription and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              Sales_Rep_Lower__c <= :Discount and Sales_Rep_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel1 : Level1)
     {
       L1Count = ((Integer)arLevel1.get('Level1Count'));
       
     }         
                                     
    return L1Count;
  }
  
   /* Subscription Discount Level 2 */
   public static Integer SubLevel2(Decimal Subscription, Decimal ACV, Decimal Discount) {
    
   Integer L2Count;  
   
   List<AggregateResult> Level2 = [select count(id) Level2Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Subscription' and 
              Subscription_Term__c = :Subscription and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              Direct_Manager_Lower__c <= :Discount and Direct_Manager_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel2 : Level2)
     {
      L2Count = ((Integer)arLevel2.get('Level2Count'));  
     }        
                                      
    return L2Count;
  }
  
   /* Subscription Discount Level 3 */
   public static Integer SubLevel3(Decimal Subscription, Decimal ACV, Decimal Discount) {
    
   Integer L3Count;  
   
   List<AggregateResult> Level3 = [select count(id) Level3Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Subscription' and 
              Subscription_Term__c = :Subscription and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              SVP_Lower__c <= :Discount and SVP_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel3 : Level3)
     {
      L3Count = ((Integer)arLevel3.get('Level3Count'));
     }        
                                      
    return L3Count;
  }
  
  /* Subscription Discount Level 4 */
  public static Integer SubLevel4(Decimal Subscription, Decimal ACV, Decimal Discount) {
    
   Integer L4Count;  

   List<AggregateResult> Level4 = [select count(id) Level4Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Subscription' and 
              Subscription_Term__c = :Subscription and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              CEO_CFO__c <= :Discount and CEO_CFO_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel4 : Level4)
     {
      L4Count = ((Integer)arLevel4.get('Level4Count'));
     }        
                                      
    return L4Count;
  }
  
  /* Service Discount Level 1 */
  public static Integer SerLevel1(Decimal ACV,Decimal Discount) {
    
   Integer L1Count;  
   
   List<AggregateResult> Level1 = [select count(id) Level1Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Services' and 
              Subscription_Term__c = 1 and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              Sales_Rep_Lower__c <= :Discount and Sales_Rep_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel1 : Level1)
     {
      L1Count = ((Integer)arLevel1.get('Level1Count'));
     }        
                                      
    return L1Count;
  }
  
  /* Service Discount Level 2 */
    public static Integer SerLevel2(Decimal ACV,Decimal Discount) {
    
   Integer L2Count;  
   
   List<AggregateResult> Level2 = [select count(id) Level2Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Services' and 
              Subscription_Term__c = 1 and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              Direct_Manager_Lower__c <= :Discount and Direct_Manager_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel2 : Level2)
     {
      L2Count = ((Integer)arLevel2.get('Level2Count'));
     }        
                                      
    return L2Count;
  }


  /* Service Discount Level 3 */
    public static Integer SerLevel3(Decimal ACV,Decimal Discount) {
    
   Integer L3Count;  
   
   List<AggregateResult> Level3 = [select count(id) Level3Count
              from Discount_Schedule_Matrix__c
              where
              Type__c = 'Services' and 
              Subscription_Term__c = 1 and
              ACV_Lower__c <= :ACV  and ACV_Upper__c >= :ACV and
              SVP_Lower__c <= :Discount and SVP_Upper__c >= :Discount];
              
              
    for (AggregateResult arLevel3 : Level3)
     {
      L3Count = ((Integer)arLevel3.get('Level3Count'));
     }        
                                      
    return L3Count;
  }
  
  
    /* Service Discount Level 3 */
    public static Decimal GetSubscriptionTerm(list<Id> Quoteid ) {
     
     Decimal SubscriptionTerm;
     
    /* Get Subscription Term */ 
    List<AggregateResult> MaxSubscrioptionYear = [select max(Subscription_Years__c) QSubscriptionYear 
                                                  from QuoteLineItem where quoteid in :Quoteid]; 
  
    
    for (AggregateResult GetSubscriptionYear : MaxSubscrioptionYear)
    { 
     SubscriptionTerm = (Decimal)GetSubscriptionYear.get('QSubscriptionYear');
      }
 
       return SubscriptionTerm;
    }
    
    
    
    
    
}
Trigger
trigger GenQuoteApproval on QuoteLineItem (After Insert, After Update, After Delete) {

  Decimal LineMaxDiscount;
  Decimal LineMaxACV;
  Decimal SubLineMaxDiscount;
  Decimal SubLineMaxACV;
  Decimal SerLineMaxDiscount;
  Decimal SerLineMaxACV;
  Map<ID, Quote> ParentQuote = new Map<ID, Quote>();
  String SALREPID;
  String MRGID;
  String SALID;
  String CFOID;
  String Level;
  Integer GSublevel1Count;
  Integer GSublevel2Count;
  Integer GSublevel3Count;
  Integer GSublevel4Count;
  Integer GSerlevel1Count;
  Integer GSerlevel2Count;
  Integer GSerlevel3Count;
  Decimal SubscriptionTerm;
 
Try 
{
if ( Trigger.isAfter)
  {
 List<Id> listIds = new List<Id>();
 //Set<Id> listIds = new Set<Id>();

 List<Quote> QuotetList = new List<Quote>();

 /* Get Quote ID */
 for (QuoteLineItem childquoteline :  Trigger.new)
 {
        listIds.add(childquoteline.QuoteId);
    }

 ParentQuote = new Map<Id, Quote>([SELECT id,Level_1__c,Level_2__c,Level_3__c,Level_4__c FROM Quote WHERE ID IN :listIds]);


  /* Get service list of all quote id */
  list<id> serviceid = new list<id>();
  for(QuoteLineItem getserviceid : [select id  from  QuoteLineItem
                                     where quoteid in :listIds and
                                           product2.Productcode  like 'CBSVC%'] )
   {
      serviceid.add(getserviceid.id);

   }

  /* Get subscription list of all quote id */
  list<id> subscriptionid = new list<id>();
  for(QuoteLineItem getsubscriptionid : [select id  from  QuoteLineItem
                                     where quoteid in :listIds and
                                           Subscription_Terms__c <> 0] )
   {
      subscriptionid.add(getsubscriptionid.id);

   }

 /* Subscription Discount and ACV */
    List<AggregateResult> MaxSubscription = [select max(Discount_Percent__c) SubQuoteLineMaxDiscount,sum(f_ACV__c) SubQuoteLineMaxACV
                                                   from  QuoteLineItem
                                                   where quoteid in :listIds and Subscription_Terms__c <> 0 and
                                                         ID not in :serviceid
                                                 ];

  for (AggregateResult SubQuoteMaxDiscount : MaxSubscription)
  {
  SubLineMaxDiscount = (Decimal)SubQuoteMaxDiscount.get('SubQuoteLineMaxDiscount');
  SubLineMaxACV = (Decimal)SubQuoteMaxDiscount.get('SubQuoteLineMaxACV');
  }

   system.debug('Subscription Line Discount  :' + SubLineMaxDiscount);
   system.debug('Subscription Line ACV  :' + SubLineMaxACV);

   /* Service Discount and ACV */
  List<AggregateResult> MaxService = [select max(Discount_Percent__c) SerQuoteLineMaxDiscount,SUM(f_ACV__c) SerQuoteLineMaxACV
                                      from  QuoteLineItem
                                      where quoteid in :listIds and product2.Productcode  like 'CBSVC%' and
                                            id not in :subscriptionid];

  for (AggregateResult SerQuoteMaxDiscount : MaxService)
  {
  SerLineMaxDiscount = (Decimal)SerQuoteMaxDiscount.get('SerQuoteLineMaxDiscount');
  SerLineMaxACV = (Decimal)SerQuoteMaxDiscount.get('SerQuoteLineMaxACV');
  }

   system.debug('Service Line Discount  :' + SerLineMaxDiscount);
   system.debug('Service Line ACV  :' + SerLineMaxACV);

  Opportunity Opp = [select ownerid
                    from opportunity
                    where
                    id in (select OpportunityId from quote where id in :listIds) ];

  system.debug(listIds);
  
  SubscriptionTerm = ApprovalUtils.GetSubscriptionTerm(listIds);

  system.debug('Get New Subscription Term' + SubscriptionTerm);
  
  /* Get Manager ID */
  User Usr = [select managerid from user where id = :opp.ownerid];

 /* Subscription Query to get level 1..4 values */
   if ( SubscriptionTerm != null &&
        SubLineMaxACV != null &&
        SubLineMaxDiscount != null)
   {
       GSublevel1Count = ApprovalUtils.SubLevel1(SubscriptionTerm , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level1 :' + GSublevel1Count);

       GSublevel2Count = ApprovalUtils.SubLevel2(SubscriptionTerm , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level2 :' + GSublevel2Count);

       GSublevel3Count = ApprovalUtils.SubLevel3(SubscriptionTerm , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level3 :' + GSublevel3Count);

       GSublevel4Count = ApprovalUtils.SubLevel4(SubscriptionTerm , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level4 :' + GSublevel4Count);

        
   }

    /* Service Query to get level 1..4 values */
   if (SerLineMaxACV != null && SerLineMaxDiscount != null)
    {
       GSerlevel1Count = ApprovalUtils.SerLevel1(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level1 :' + GSerlevel1Count);

        GSerlevel2Count = ApprovalUtils.SerLevel2(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level2 :' + GSerlevel2Count);

        GSerlevel3Count = ApprovalUtils.SerLevel3(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level3 :' + GSerlevel3Count);
    }    
   
   
   If( GSublevel1Count >= 1  || GSerlevel1Count >= 1  )
     {
      SALREPID = Opp.OwnerId;
      }   
     
     
    If (GSublevel2Count >= 1 || GSerlevel2Count >= 1  )
    {
      SALREPID = NULL;
      MRGID = Usr.managerid;
    }
       
    
    /* Future here you may have to change for amount > 1000000 if its going only to SVP */
   If ( GSublevel3Count >= 1 || GSerlevel3Count >= 1)
      {
      SALREPID = NULL;
      MRGID = Usr.managerid;
      SALID = '00580000007jaoA';
      }
      
    If ( GSublevel4Count >= 1 )
    {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = '00580000006HV0w';
    Level = '4sub';
    }  
  
    //system.debug('Which Level :' + Level);
    system.debug('Sales Rep :' + SALREPID);
    system.debug('Manager :' + MRGID);
    system.debug('Sales Ops :' + SALID);
    system.debug('CEO CFO :'  + CFOID);

for (QuoteLineItem gqtl : Trigger.new)
 {
   Quote MyParentQuote = ParentQuote.get(gqtl.QuoteId);

    MyParentQuote.Test_Sudhir__c = String.valueOf(LineMaxDiscount);
    MyParentQuote.Test_Sudhir_ACV__c = String.valueOf(LineMaxACV);
    MyParentQuote.Level_1__c = SALREPID;
    MyParentQuote.Level_2__c=MRGID;
    MyParentQuote.Level_3__c=SALID;
    MyParentQuote.Level_4__c=CFOID;
    MyParentQuote.Test_Sudhir_Level__c=Level;
  }

  update ParentQuote.values();

 }
}

catch(Exception e) {
    System.debug('The following exception has occurred: ' + e.getMessage());    
}
  
}


 
George AdamsGeorge Adams
What have you done so far on the test class? Or are you asking someone to write it for you?
GMASJGMASJ
Hi George, 

  I wrote below test class for trigger and apex class both in a single test class 

 
@isTest(SeeAllData = true)
private class ApprovalUtils_Test {

        static testMethod void Approval(){
 
                 test.startTest();
                 
                 ApprovalUtils.SubLevel1(1,0,0);
                 ApprovalUtils.SubLevel2(1,0,10);
                 ApprovalUtils.SubLevel3(1,0,11);
                 ApprovalUtils.SubLevel4(1,0,41);
               
                 ApprovalUtils.SerLevel1(1,1);
                 ApprovalUtils.SerLevel2(1,10);
                 ApprovalUtils.SerLevel3(1,41);
                 
                 List<Account> lstAccount =   new List<Account>{
                          new Account(name = 'samarth', Email_Domain__c = 'samarth.com')

                         };
                         
                 insert lstAccount;
         

                 List<Opportunity> lstOpportunity =   new List<Opportunity>{
                     new Opportunity(AccountId = lstAccount[0].id,name='sudhir',type='New Customer',closedate=System.today(),StageName='Booked',ForecastCategoryName='Closed',Pricebook2Id='01s800000007nNeAAI')
                  }; 
                 insert lstOpportunity;
                 
                 List<Quote> lstQuote =   new List<Quote>{
                     new Quote(OpportunityId = lstOpportunity[0].id,name='sudhirquote')
                 };  
                 insert lstQuote;
                 
                 //Product2 prod = [select id,name from product2 where name = 'Couchbase Enterprise Edition Server /M Gold'];
 
                 
                 List<QuoteLineItem> lstQuotelineitem =   new List<QuoteLineItem>{
                     new QuoteLineItem(QuoteId = lstQuote[0].id,discount=1,Quantity=1,PricebookEntryId='01u8000000CrSczAAF' , UnitPrice=8316, Product2Id='01t800000049k93AAA')
                 };  
                 insert lstQuotelineitem;
                 
                 
                 
                 test.stopTest();
     }     
 }

I am having issue with last item inserting into quotelineitem I keep gettng below error Please suggest how to fix this issue

List<QuoteLineItem> lstQuotelineitem = new List<QuoteLineItem>{ new QuoteLineItem(QuoteId = lstQuote[0].id,discount=1,Quantity=1,PricebookEntryId='01u8000000CrSczAAF' , UnitPrice=8316, Product2Id='01t800000049k93AAA') }; insert lstQuotelineitem;

Thanks
Sudhir
George AdamsGeorge Adams
 What's the error you're getting for that part?
GMASJGMASJ

Hi George, 

 I get below error sorry missed to paste this error in earlier thread

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is in a different pricebook than the one assigned to the Quote, or Quote has no pricebook assigned.: [PricebookEntryId]

Thanks
Sudhir

GMASJGMASJ
Hi George, 

  Can you suggest me how to write test class logic for trigger I am not able to get code coverage at all it shows 0% 

Thanks
Sudhir