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 

Number Of Revenue Installments in test trigger

Hi everybody,

I have to create a test code trigger to coverage my case trigger. But When I test it, I have this error : caused by: System.NullPointerException: Argument cannot be null.
In fact I have to fill the Number Of Revenue Installments and I don't know to do that when I create the product in my test.

Here is my test trigger : 
@isTest
private class TestCreateBillingCaseonCase2 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity() {
        // 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='0012500000dVUEw',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0032500000YR74S',Unit_price_ex_VAT_per_room_booked__c=2,OpportunityId=opp.Id);
        insert QUOT;
        
        opp.SyncedQuoteId=QUOT.Id;
        opp.StageName='Closed Won';
        update opp;
        
        Pricebook2 pb22 = new Pricebook2(Name='testDIE training');
        insert pb22;

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

        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id);     
        insert OLI;

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

        Test.stopTest();
    }
    
}
And here the trigger line where is my error :
Amount__c = (OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c,
Thanks very much
Thomas
 
Nayana KNayana K
Please post trigger code as well. On which line of test class, you are recieving the error?
Thomas CailletThomas Caillet
Here is my trigger
trigger CreateBillingCaseAfterKickOff on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Kick off') && (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>();
                                
                                for(Quote QUOT : [SELECT IsSyncing, Unit_price_ex_VAT_per_room_booked__c, Monthly_billing_maximum__c, Monthly_billing_minimum__c, OpportunityId from Quote where OpportunityId =: trigger.new[0].Opportunity_Case__c AND IsSyncing = true AND Unit_price_ex_VAT_per_room_booked__c != null]){
                                case c2=new case(
                                        Subject='New hotel : Invoicing : Smart Booking Engine (Per room booked)',
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Accounting_first_invoicing_date__c = date.TODAY(),
                                        Accounting_Status__c = 'Démarrage facturation',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Accounting_Billing_Type__c = 'Per room booked',
                                        Monthly_billing_maximum_SBE__c = QUOT.Monthly_billing_maximum__c,
                                        Monthly_billing_minimum_SBE__c = QUOT.Monthly_billing_minimum__c,
                                        Amount__c = QUOT.Unit_price_ex_VAT_per_room_booked__c,
                                        CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
                                        insert c2;
                                }
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, CurrencyIsoCode, Billing_Type__c, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c = 'Monthly'])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        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;
                                        
                                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 It's on line 50 (in the trigger not the test) that I have an error.

Thanks for your quick answer
Nayana KNayana K
Change 
OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id);

to
OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id, Number_Of_Revenue_Installments__c = 1);
once and check. I think without populating Number_Of_Revenue_Installments__c field, it is null. So it will evaluate as 
Amount__c = (OLI.TotalPrice)/null which gives attempt to deference null error.
Thomas CailletThomas Caillet
Thanks but I have already try this and I have the same error :/
Do I did something wrong ?
Nayana KNayana K
trigger CreateBillingCaseAfterKickOff on Case (after update) 
{

	if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel : Kick off') && (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>();
		
		for(Quote QUOT : [SELECT IsSyncing, Unit_price_ex_VAT_per_room_booked__c, 
								Monthly_billing_maximum__c, Monthly_billing_minimum__c, 
								OpportunityId 
							from Quote 
							where OpportunityId =: trigger.new[0].Opportunity_Case__c 
								AND IsSyncing = true 
								AND Unit_price_ex_VAT_per_room_booked__c != null]){
		case c2=new case(
				Subject='New hotel : Invoicing : Smart Booking Engine (Per room booked)',
				OwnerId='00G20000001HboH', 
				AccountId = trigger.new[0].AccountId, 
				Accounting_first_invoicing_date__c = date.TODAY(),
				Accounting_Status__c = 'Démarrage facturation',
				Accounting_request_type__c = 'Paramétrage module facturation',
				Alfred_Scope__c = 'Billing',
				Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
				Subject__c = 'Accounting',
				Type = 'Action Request',
				Origin ='Automatic case',
				Accounting_Billing_Type__c = 'Per room booked',
				Monthly_billing_maximum_SBE__c = QUOT.Monthly_billing_maximum__c,
				Monthly_billing_minimum_SBE__c = QUOT.Monthly_billing_minimum__c,
				Amount__c = QUOT.Unit_price_ex_VAT_per_room_booked__c,
				CurrencyIsoCode = trigger.new[0].CurrencyIsoCode);
				insert c2;
		}
		// changed this line
		for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2Id, PricebookEntry.Product2.Name, CurrencyIsoCode, Billing_Type__c, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c AND PricebookEntry.Product2.Billing_Type__c = 'Monthly'])
			 {
			
				String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
				
				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.Number_Of_Revenue_Installments__c != null && OLI.TotalPrice != null) ? ((OLI.TotalPrice)/OLI.Number_Of_Revenue_Installments__c) : 0,
				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;

	}
}
changed 2 lines. Please check once and let me know.

NOTE:Always try to write bulkified trigger.
Thomas CailletThomas Caillet
Thanks Nayana that's works good.
Just for me,me question was : can I write the number of revenue installment in my test trigger when I create the product ??
Nayana KNayana K
OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id, Number_Of_Revenue_Installments__c = 1);

here as I did, you can populate revenue installment field. ..