• bastian
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi All,

I've written and deployed several triggers now, but I seem to be having trouble with one Trigger that keeps saying that it's only covered 72% by test unit. I've included the trigger and the testcase. Could anybody help me please. 

What the trigger does is it calculates the total rate on insertion order record, and sum them to quota's monthly field. So that we can easily see our salesperson's projected and actual quotas.

It is working perfectly on our sandbox environment, but I can't get the test case right to be able to deploy it.


Here's the trigger:
trigger InsertQuotaBefore on Quota__c (before insert, before update) {
    Integer START_FISCAL_MONTH = 9; //September
   
    Integer startYear = 0;
    Integer nextYear = 0;
   
    Trigger.new[0].Jan_Actual__c = 0;
    Trigger.new[0].Feb_Actual__c = 0;
    Trigger.new[0].Mar_Actual__c = 0;
    Trigger.new[0].Apr_Actual__c = 0;
    Trigger.new[0].May_Actual__c = 0;
    Trigger.new[0].Jun_Actual__c = 0;
    Trigger.new[0].Jul_Actual__c = 0;
    Trigger.new[0].Aug_Actual__c = 0;
    Trigger.new[0].Sept_Actual__c = 0;
    Trigger.new[0].Oct_Actual__c = 0;
    Trigger.new[0].Nov_Actual__c = 0;
    Trigger.new[0].Dec_Actual__c = 0;
   
    String startYearStr = Trigger.new[0].Financial_Year__c.substring( 0, 4 );
   
    startYear = Integer.valueOf( startYearStr );
    nextYear = startYear + 1;   

    Insertion_Order__c [] ios = [select Id, CreatedDate, Total_Rate_after_disc__c, created_year__c, created_month__c from Insertion_Order__c where Sales_Person__c= :Trigger.new[0].Sales_Person__c and (( created_year__c = :startYear and created_month__c >= :START_FISCAL_MONTH ) or ( created_year__c = :nextYear and created_month__c < :START_FISCAL_MONTH ))];
               
    for ( Integer i = 0; i < ios.size(); i++ ) {
        if ( ios[i].created_month__c == 1 ) {
            Trigger.new[0].Jan_Actual__c = Trigger.new[0].Jan_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 2 ) {
            Trigger.new[0].Feb_Actual__c = Trigger.new[0].Feb_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 3 ) {
            Trigger.new[0].Mar_Actual__c = Trigger.new[0].Mar_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 4 ) {
            Trigger.new[0].Apr_Actual__c = Trigger.new[0].Apr_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 5 ) {
            Trigger.new[0].May_Actual__c = Trigger.new[0].May_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 6 ) {
            Trigger.new[0].Jun_Actual__c = Trigger.new[0].Jun_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 7 ) {
            Trigger.new[0].Jul_Actual__c = Trigger.new[0].Jul_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 8 ) {
            Trigger.new[0].Aug_Actual__c = Trigger.new[0].Aug_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 9 ) {
            Trigger.new[0].Sept_Actual__c = Trigger.new[0].Sept_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 10 ) {
            Trigger.new[0].Oct_Actual__c = Trigger.new[0].Oct_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 11 ) {
            Trigger.new[0].Nov_Actual__c = Trigger.new[0].Nov_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 12 ) {
            Trigger.new[0].Dec_Actual__c = Trigger.new[0].Dec_Actual__c + ios[i].Total_Rate_after_disc__c;
        }   
    }
}



And here's my test class:
public class InsertQuotaBeforeTestCase {
    static testMethod void testInsertQuotaBefore() {
        Date dt = System.today();

        Insertion_Order__c io1 = new Insertion_Order__c( E_Campaign_Start_Date__c = dt,
            Contract_Period__c = 3,
            Sales_Person__c = '00520000000whV2AAI',
            CCRID__c = '1234567890',
            Account_Number__c = '1234567890',
            Type__c = 'New Business',
            Invoice_Status__c = 'Pending',
            Order_Status__c = 'Draft'
        );
           
        insert io1;
        Insertion_Order__c [] ios = [select Contract_Period__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Contract_Period__c, 3 );

        InvoiceOB__c[] inList = [select Id from InvoiceOB__c where Insertion_Order_No__c= :io1.Id];       
        System.assertEquals( inList.size(), 0 );

        io1.Order_Status__c = 'Signed';
        update io1;
       
        inList = [select Id from InvoiceOB__c where Insertion_Order_No__c= :io1.Id];
        System.assertEquals( inList.size(), 1 );

        Order_Details__c od1 = new Order_Details__c( Name = '01',
            Product_Category__c = 'Directory Listing',
            Unit_Price__c = 800,
            CurrencyIsoCode = 'SGD',
            Quantity__c = 3,
            Insertion_Order__c = io1.Id );

        insert od1;

        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 2400 );
           
        Order_Details__c od2 = new Order_Details__c( Name = '02',
            Product_Category__c = 'Directory Listing',
            Unit_Price__c = 200,
            CurrencyIsoCode = 'SGD',
            Quantity__c = 3,
            Insertion_Order__c = io1.Id );
           
        insert od2;
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
       
        Quota__c qu1 = new Quota__c( Name = '01',
            Financial_Year__c = '2007/08',
            Sales_Person__c = '00520000000whV2AAI' );
   
        insert qu1;
        Quota__c[] quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 3000 );

       
        update qu1;
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 3000 );

        ios = [select Total_Rate_after_disc__c, created_month__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
        System.assertEquals( ios[0].created_month__c, 4 );
       
        io1.CCRID__C = '9876543210';
        update io1;
       
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
       
        od2.Unit_Price__c = 300;
        update od2;
       
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3300 );
       
        delete od2;
       
        ios = [select Total_Rate_after_disc__c, created_month__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 2400 );
       
        quotas = [select Id, Sales_Person__c, Sept_Actual__c, Oct_Actual__c, Nov_Actual__c, Dec_Actual__c, Jan_Actual__c, Feb_Actual__c, Mar_Actual__c, Apr_Actual__c, May_Actual__c, Jun_Actual__c, Jul_Actual__c, Aug_Actual__c, Q1_Actual__c, Q2_Actual__c, Q3_Actual__c, Q4_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];


        System.assertEquals( quotas[0].Q3_Actual__c, quotas[0].Mar_Actual__c + quotas[0].Apr_Actual__c + quotas[0].May_Actual__c);
        Quota__c qu2 = new Quota__c( Name = '01',
            Financial_Year__c = '2008/09',
            Sales_Person__c = '00520000000whV2AAI' );
           
        insert qu2;
       
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Id = :qu2.Id];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 0 );
       
        io1.

        delete io1;
        update qu1;
       
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 2 );
        System.assertEquals( quotas[0].Apr_Actual__c, 0 );
       
       
      }
}


Thank you before hand for your help
Bastian

Hi All,

I've written and deployed several triggers now, but I seem to be having trouble with one Trigger that keeps saying that it's only covered 72% by test unit. I've included the trigger and the testcase. Could anybody help me please. 

What the trigger does is it calculates the total rate on insertion order record, and sum them to quota's monthly field. So that we can easily see our salesperson's projected and actual quotas.

It is working perfectly on our sandbox environment, but I can't get the test case right to be able to deploy it.


Here's the trigger:
trigger InsertQuotaBefore on Quota__c (before insert, before update) {
    Integer START_FISCAL_MONTH = 9; //September
   
    Integer startYear = 0;
    Integer nextYear = 0;
   
    Trigger.new[0].Jan_Actual__c = 0;
    Trigger.new[0].Feb_Actual__c = 0;
    Trigger.new[0].Mar_Actual__c = 0;
    Trigger.new[0].Apr_Actual__c = 0;
    Trigger.new[0].May_Actual__c = 0;
    Trigger.new[0].Jun_Actual__c = 0;
    Trigger.new[0].Jul_Actual__c = 0;
    Trigger.new[0].Aug_Actual__c = 0;
    Trigger.new[0].Sept_Actual__c = 0;
    Trigger.new[0].Oct_Actual__c = 0;
    Trigger.new[0].Nov_Actual__c = 0;
    Trigger.new[0].Dec_Actual__c = 0;
   
    String startYearStr = Trigger.new[0].Financial_Year__c.substring( 0, 4 );
   
    startYear = Integer.valueOf( startYearStr );
    nextYear = startYear + 1;   

    Insertion_Order__c [] ios = [select Id, CreatedDate, Total_Rate_after_disc__c, created_year__c, created_month__c from Insertion_Order__c where Sales_Person__c= :Trigger.new[0].Sales_Person__c and (( created_year__c = :startYear and created_month__c >= :START_FISCAL_MONTH ) or ( created_year__c = :nextYear and created_month__c < :START_FISCAL_MONTH ))];
               
    for ( Integer i = 0; i < ios.size(); i++ ) {
        if ( ios[i].created_month__c == 1 ) {
            Trigger.new[0].Jan_Actual__c = Trigger.new[0].Jan_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 2 ) {
            Trigger.new[0].Feb_Actual__c = Trigger.new[0].Feb_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 3 ) {
            Trigger.new[0].Mar_Actual__c = Trigger.new[0].Mar_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 4 ) {
            Trigger.new[0].Apr_Actual__c = Trigger.new[0].Apr_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 5 ) {
            Trigger.new[0].May_Actual__c = Trigger.new[0].May_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 6 ) {
            Trigger.new[0].Jun_Actual__c = Trigger.new[0].Jun_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 7 ) {
            Trigger.new[0].Jul_Actual__c = Trigger.new[0].Jul_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 8 ) {
            Trigger.new[0].Aug_Actual__c = Trigger.new[0].Aug_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 9 ) {
            Trigger.new[0].Sept_Actual__c = Trigger.new[0].Sept_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 10 ) {
            Trigger.new[0].Oct_Actual__c = Trigger.new[0].Oct_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 11 ) {
            Trigger.new[0].Nov_Actual__c = Trigger.new[0].Nov_Actual__c + ios[i].Total_Rate_after_disc__c;
        } else if ( ios[i].created_month__c == 12 ) {
            Trigger.new[0].Dec_Actual__c = Trigger.new[0].Dec_Actual__c + ios[i].Total_Rate_after_disc__c;
        }   
    }
}



And here's my test class:
public class InsertQuotaBeforeTestCase {
    static testMethod void testInsertQuotaBefore() {
        Date dt = System.today();

        Insertion_Order__c io1 = new Insertion_Order__c( E_Campaign_Start_Date__c = dt,
            Contract_Period__c = 3,
            Sales_Person__c = '00520000000whV2AAI',
            CCRID__c = '1234567890',
            Account_Number__c = '1234567890',
            Type__c = 'New Business',
            Invoice_Status__c = 'Pending',
            Order_Status__c = 'Draft'
        );
           
        insert io1;
        Insertion_Order__c [] ios = [select Contract_Period__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Contract_Period__c, 3 );

        InvoiceOB__c[] inList = [select Id from InvoiceOB__c where Insertion_Order_No__c= :io1.Id];       
        System.assertEquals( inList.size(), 0 );

        io1.Order_Status__c = 'Signed';
        update io1;
       
        inList = [select Id from InvoiceOB__c where Insertion_Order_No__c= :io1.Id];
        System.assertEquals( inList.size(), 1 );

        Order_Details__c od1 = new Order_Details__c( Name = '01',
            Product_Category__c = 'Directory Listing',
            Unit_Price__c = 800,
            CurrencyIsoCode = 'SGD',
            Quantity__c = 3,
            Insertion_Order__c = io1.Id );

        insert od1;

        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 2400 );
           
        Order_Details__c od2 = new Order_Details__c( Name = '02',
            Product_Category__c = 'Directory Listing',
            Unit_Price__c = 200,
            CurrencyIsoCode = 'SGD',
            Quantity__c = 3,
            Insertion_Order__c = io1.Id );
           
        insert od2;
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
       
        Quota__c qu1 = new Quota__c( Name = '01',
            Financial_Year__c = '2007/08',
            Sales_Person__c = '00520000000whV2AAI' );
   
        insert qu1;
        Quota__c[] quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 3000 );

       
        update qu1;
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 3000 );

        ios = [select Total_Rate_after_disc__c, created_month__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
        System.assertEquals( ios[0].created_month__c, 4 );
       
        io1.CCRID__C = '9876543210';
        update io1;
       
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3000 );
       
        od2.Unit_Price__c = 300;
        update od2;
       
        ios = [select Total_Rate_after_disc__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 3300 );
       
        delete od2;
       
        ios = [select Total_Rate_after_disc__c, created_month__c from Insertion_Order__c where Id = :io1.Id];
        System.assertEquals( ios[0].Total_Rate_after_disc__c, 2400 );
       
        quotas = [select Id, Sales_Person__c, Sept_Actual__c, Oct_Actual__c, Nov_Actual__c, Dec_Actual__c, Jan_Actual__c, Feb_Actual__c, Mar_Actual__c, Apr_Actual__c, May_Actual__c, Jun_Actual__c, Jul_Actual__c, Aug_Actual__c, Q1_Actual__c, Q2_Actual__c, Q3_Actual__c, Q4_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];


        System.assertEquals( quotas[0].Q3_Actual__c, quotas[0].Mar_Actual__c + quotas[0].Apr_Actual__c + quotas[0].May_Actual__c);
        Quota__c qu2 = new Quota__c( Name = '01',
            Financial_Year__c = '2008/09',
            Sales_Person__c = '00520000000whV2AAI' );
           
        insert qu2;
       
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Id = :qu2.Id];
        System.assertEquals( quotas.size(), 1 );
        System.assertEquals( quotas[0].Apr_Actual__c, 0 );
       
        io1.

        delete io1;
        update qu1;
       
        quotas = [select Sales_Person__c, Apr_Actual__c from Quota__c where Sales_Person__c = '00520000000whV2AAI'];
        System.assertEquals( quotas.size(), 2 );
        System.assertEquals( quotas[0].Apr_Actual__c, 0 );
       
       
      }
}


Thank you before hand for your help
Bastian