function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thomas CailletThomas Caillet 

Test Trigger Code Coverage

Hello,

I tried to code my test trigger apex class but I have an error with the code coverage.
Here is my trigger :
 
trigger CreateBillingCaseAfterRegistration on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
                                List<Quote> Quo=new List<Quote>();
                                List<Case> CaseOpp =new List<Case>();
                                
                                case c1=new case(
                                        Subject='New hotel : Kick off',
                                        OwnerId='00Gw0000002lubt', 
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Subject__c = 'New Hotel',
                                        Type = 'Action Request', 
                                        Training_type__c = 'Kick-Off Call',
                                        Kick_off_date__c=trigger.new[0].Kick_off_date__c, 
                                        Origin ='Automatic case',                                     
                                        AccountId = trigger.new[0].AccountId,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                     
                              insert CaseOpp;
                              
                               case c2=new case(
                                        Subject='New hotel : Add / Modify Channel : Start Date Invoicing',
                                        OwnerId='00G20000001HboH',
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_first_invoicing_date__c = trigger.new[0].Opportunity_Start_Date__c.addDays(61),
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND ((PricebookEntry.Product2.Name LIKE '%Set Up Fee%') OR (PricebookEntry.Product2.Name LIKE '%training%'))])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly'){
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                        
                                        else{
                                        case c3=new case(
                                        Subject='New hotel : Invoicing :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId,
                                        Opportunity_Case__c=trigger.new[0].Opportunity_Case__c,
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate, 
                                        Accounting_Status__c = 'Facturation Set-up fees',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = OLI.Billing_Type__c,
                                        Amount__c = OLI.TotalPrice,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;
                                        }
                                
                                        
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Date__c = datetime.now(),
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                     
                                     }

                                insert Feat;

                }
}
And here my test class:
 
@isTest
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity2() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
       
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId='0012500000e1oU4',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;
        
        OpportunityLineItem OLI = new OpportunityLineItem (UnitPrice=300,opportunityId = opp.Id,Quantity=1,PricebookEntryId='01uw000000rrJ5F');     
        insert OLI;          
        
        Quote QUOT = new Quote (Name='TEST',BillingPostalCode = '75009' ,BillingCountry = 'France' ,ContactId='0032500000ZL882',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;

        Case ca = new Case(Subject='New hotel : Availpro Registration',AccountId = opp.AccountId, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        // Perform test
        Test.startTest(); 
        
        ca.Status='Closed';
        update ca;

        Test.stopTest();
    }
    
}
My test code is okay up to the for loop but I can't enter in for loop.
For information, My pricebookentry contain "Set up fee" in the product name.

Thanks for your help
Thomas
Gopal AgrawalGopal Agrawal
Hi Thomas,

I have cross checked your code.

Please try with

@istest(SeeAllData=true)

 
Amit Chaudhary 8Amit Chaudhary 8
Try to update your code like below
@isTest
private class TestCreateBillingCaseonCase 
{
    @isTest static void TestCreateBillingCaseonCaseWithOpportunity2() 
	{
       
	    Account acc = new Account();
			acc.name ='Test';
		insert acc;
		
		Contact cont = new Contact();
			cont.FirstName ='Test';
			cont.LastName ='TestLastName';
			cont.accountid= acc.id;
		insert cont;
		

		Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
		insert pb22;

		Product2 pro2 = new Product2(Name='training', isActive=true);
		insert pro2;

		PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
											 isActive=true,UseStandardPrice = false);
		insert pbe2;

		
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Prospecting',
                                       AccountId=acc.id,
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;
        
		
		
		
        OpportunityLineItem OLI = new OpportunityLineItem (UnitPrice=300,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.id);     
        insert OLI;          
        
        Quote QUOT = new Quote (Name='TEST',BillingPostalCode = '75009' ,BillingCountry = 'France' ,ContactId=cont.id,Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;

        Case ca = new Case(Subject='New hotel : Availpro Registration',AccountId = acc.id, Status ='in progress',Opportunity_Case__c = opp.Id);
        insert ca;     
        
        Test.startTest(); 
        
			ca.Status='closed';
			update ca;

        Test.stopTest();
    }
    
}


Let us know if this will help you
Thomas CailletThomas Caillet
Hello,

Thanks for your quick answers but still doesn't work.
Amit for your solution I have lot of validation rules so I can't create all objects in one test code.

Thanks
Thomas CailletThomas Caillet
In fact is not my code it's just the statement in my for loop that doesn't work : ((PricebookEntry.Product2.Name LIKE '%Set Up Fee%') OR (PricebookEntry.Product2.Name LIKE '%training%'))
Because when I try to add "NOT" before pricebookentry that's works but I can't understand why because my Product2.Name contain Set up Fee in his name.
Maybe I not use correctly the like statement could you help me please ?