1

I have Apex class that gets called from Process Builder to update a record. I have 89% code coverage but when I try to deploy its complaining it doesnt have enough code coverage as its failing at the first assertion itself.

I could have gone with a trigger but want to make it future proof as the logic keeps changes so have to use Process Builder.

Can anyone help?

Thanks

public class CalcCRBusinessHours {
    @InvocableMethod
    public static void getUpdateBusHours(List<getrec> rec){
        datetime createdDt;
        decimal busHrs;

        List<Credit_Request__c> CreditR=[select Opportunity__c, Sales_Resubmit_Date__c, Cancelled_On__c, 
                                         Opportunity__r.Credit_Process_Started_on__c, 
                                         Opportunity__r.Last_Credit_Decision_Made_On__c, 
                                         Credit_Decision_Changed_On__c, createddate, id, 
                                         Sales_CR_Process_Time__c, Credit_Support_CR_Process_Time__c 
                                         from Credit_Request__c where id =  :rec[0].recid];

        List<Opportunity> opty = [select id, Credit_Support_CR_Time_Tracker__c, Credit_Process_Started_on__c, 
                                  Sales_CR_Time_Tracker__c, Last_Credit_Decision_Made_On__c from Opportunity 
                                  where Id = :CreditR[0].Opportunity__c] ;                

        Id businessHoursId = [select Id from BusinessHours where name = 'SLA Business Hours' ].id;
        if( rec[0].Operation == 'Credit')
        {
            for (Credit_Request__c cr:CreditR ){

                 if(cr.Sales_Resubmit_Date__c == Null)
                 {
                     createdDt = cr.createddate;
                 }
                 else
                 {
                     createdDt = cr.Sales_Resubmit_Date__c;       
                 }

                busHrs = (decimal.valueof(BusinessHours.diff(businessHoursId, createdDt, datetime.now())/1000/60)/60).setScale(2);
                if(cr.Credit_Support_CR_Process_Time__c == Null)
                {
                    cr.Credit_Support_CR_Process_Time__c = busHrs;
                }
                else
                {
                    cr.Credit_Support_CR_Process_Time__c = cr.Credit_Support_CR_Process_Time__c + busHrs;
                }
                cr.Credit_Decision_Changed_On__c = datetime.now();
            }
            for (Opportunity op:opty ){
                if(op.Credit_Support_CR_Time_Tracker__c == Null)
                {
                    op.Credit_Support_CR_Time_Tracker__c = busHrs;
                }
                else
                {
                    op.Credit_Support_CR_Time_Tracker__c = op.Credit_Support_CR_Time_Tracker__c + busHrs;
                }
            }
            System.debug('##CreditR_Credit: '+ CreditR);
            System.debug('##opty_Credit: '+ opty);
            update opty;
            update CreditR;
        }

        if( rec[0].Operation == 'Sales')
        {
            for (Credit_Request__c cr:CreditR ){   

                busHrs = (decimal.valueof(BusinessHours.diff(businessHoursId, cr.Credit_Decision_Changed_On__c, datetime.now())/1000/60)/60).setScale(2);
                if(cr.Sales_CR_Process_Time__c == Null)
                {
                    cr.Sales_CR_Process_Time__c = busHrs;
                }
                else
                {
                    cr.Sales_CR_Process_Time__c = cr.Sales_CR_Process_Time__c + busHrs;
                }
                if(rec[0].FieldChanged == 'ReSubmit')
                {
                    cr.Sales_Resubmit_Date__c = datetime.now();
                }
                if(rec[0].FieldChanged == 'Cancelled')
                {
                    cr.Cancelled_On__c = datetime.now();
                }


            }
            for (Opportunity op:opty ){
                if(op.Sales_CR_Time_Tracker__c == Null)
                {
                    op.Sales_CR_Time_Tracker__c = busHrs;
                }
                else
                {
                    op.Sales_CR_Time_Tracker__c = op.Sales_CR_Time_Tracker__c + busHrs;
                }
            }
            System.debug('##CreditR_Sales: '+ CreditR);
            System.debug('##opty_Sales: '+ opty);
            update opty;
            update CreditR;
        }

    }


    public class getrec{       

        @invocablevariable 
        public Id recid;

        @invocablevariable 
        public string Operation;

        @invocablevariable 
        public string FieldChanged;

    }
}

and here's the test class I have

@isTest
public class TestCalcCRBusinessHours {

    private static testMethod void doTest() {
        Id businessHoursId = [select Id from BusinessHours where name = 'SLA Business Hours' ].id;
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;


        Contact con = new Contact();
        con.LastName ='test';
        con.FirstName = 'test';
        con.email = 'trt@test.com';
        con.OtherCountry = 'United Sates';        
        con.AccountID = testAcct.ID;
        insert con;

        Opportunity oppt = new Opportunity();
        oppt.Name ='New mAWS Deal';
        oppt.AccountID = testAcct.ID;
        oppt.StageName = 'Generate Opportunity';
        oppt.Amount = 3000;
        oppt.CloseDate = System.today();
        insert oppt;

        Credit_Request__c cr = new Credit_Request__c();
        cr.Opportunity__c = oppt.ID;
        cr.Legal_Entity_Name__c = 'New mAWS Deal';
        cr.DUNS_number__c = '01-123-2345';
        cr.Due_Date__c = System.today();
        cr.Credit_Request_Type__c = 'New';
        cr.Max_Term_Requested_Mths__c = 36;

        insert cr;

        Datetime yesterday = Datetime.now().addDays(-1);
        Test.setCreatedDate(cr.Id, yesterday);

        cr.Credit_Decision__c = 'Approved per Credit Policy';
        update cr;

        cr = [select id, Credit_Support_CR_Process_Time__c, Credit_Decision_Changed_On__c from Credit_Request__c where DUNS_number__c = '01-123-2345' ];

        System.debug('##cr1: '+ cr);
        System.assert (cr.Credit_Support_CR_Process_Time__c != NULL);
        System.assert (cr.Credit_Decision_Changed_On__c != NULL);
        system.assertequals(cr.Credit_Support_CR_Process_Time__c,cr.Credit_Support_CR_Process_Time__c);

        cr.Resubmit__c = true;
        update cr;

        cr = [select id, Sales_CR_Process_Time__c, Sales_Resubmit_Date__c, Credit_Decision_Changed_On__c from Credit_Request__c where DUNS_number__c = '01-123-2345' ];
        System.debug('##cr2: '+ cr);
        System.assert (cr.Sales_CR_Process_Time__c != NULL);
        System.assert (cr.Sales_Resubmit_Date__c != NULL);
        system.assertequals(cr.Sales_CR_Process_Time__c,cr.Sales_CR_Process_Time__c);

        cr.Cancelled__c = true;
        update cr;

        cr = [select id, Cancelled_On__c, Sales_CR_Process_Time__c, Sales_Resubmit_Date__c, Credit_Decision_Changed_On__c from Credit_Request__c where DUNS_number__c = '01-123-2345' ];
        System.debug('##cr3: '+ cr);
        System.assert (cr.Sales_CR_Process_Time__c != NULL);
        System.assert (cr.Cancelled_On__c != NULL);
        system.assertequals(cr.Sales_CR_Process_Time__c,cr.Sales_CR_Process_Time__c);

    }

}
  • Just to be sure, the line that is failing is System.assert (cr.Credit_Support_CR_Process_Time__c != NULL);? Also, just a general tip - you should extract your calls to now() and today() into variables in your trigger handler, so that your test class can set them to a static value while it runs. That way your test's success will never be dependent on the date. – IllusiveBrian Aug 19 '17 at 23:14
  • Hi Brian, its actually failing at System.assert (cr.Credit_Decision_Changed_On__c != NULL); – cloudit Aug 19 '17 at 23:26
  • If any part of your tests rely on PB you will either need to deploy the PB first and activate it or rework your tests. PB is deployed as inactive – Eric Aug 21 '17 at 5:03
2

It doesn't appear that your test case is explicitly calling CalcCRBusinessHours.getUpdateBusHours and is instead relying on the required Process Builder configuration being active.

Since Process Builder configuration can be changed outside a deployment I'd have the test case focus solely on the code. Explicitly call CalcCRBusinessHours.getUpdateBusHours() before you start making assertions about the expected outcome. Make sure your assertions are only applicable to what you expect that method to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.