+ Start a Discussion
Thomas CailletThomas Caillet 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, caused by: System.NullPointerException: Argument cannot be null.

Hello,

I have a probleme with a test class in sandbox.
Salesforce says that my field "Number_Of_Revenue_Installments__c" (line 153 and 193) is NULL but I tried to see this field with System.Debug() and it's not NULL.

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, PricebookEntry.Product2.ProductCode, 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.ProductCode =: '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' OR PricebookEntry.Product2.ProductCode =: '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c3=new case(
                                        Subject='Deposit for'+' '+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 = 'Deposit Google Ads',
                                        Accounting_request_type__c = 'Paramétrage module facturation',
                                        Alfred_Scope__c = 'Billing',
                                        Subject__c = 'Accounting',
                                        Type = 'Action Request',
                                        Origin ='Automatic case',
                                        Description = 'Please see this doc to view the process : https://backoffice.availpro.com/search/Help/Index/HLP-258637'+'\n'+'Fee schedule :'+'\n'+'a. – of 25 rooms = 250 '+ trigger.new[0].CurrencyIsoCode +'\n'+'b. 26 to 50 rooms = 400 '+ trigger.new[0].CurrencyIsoCode +'\n'+'c. > of 50 rooms = 600 '+ trigger.new[0].CurrencyIsoCode,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c3;    
                                        
                                      }  
                                
                                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.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 = '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); 
                                     }
                                     
                                     }
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, 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.Billing_Type__c =: 'Monthly' AND (PricebookEntry.Product2.ProductCode != '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2' AND PricebookEntry.Product2.ProductCode != '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2')])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);

                                        case c4=new case(
                                        Subject=ProductName + ' must be activated on '+ OLI.ServiceDate.format(),
                                        Description='This product has been subscribed by the customer and must be activated on the date mentioned in subject. Please update date if necessary',
                                        OwnerId = '00Gw0000002m46x', 
                                        AccountId = trigger.new[0].AccountId,
                                        Alfred_Scope__c = 'Configuration - Hotel',
                                        Opportunity_Case__c = trigger.new[0].Opportunity_Case__c,
                                        Subject__c = 'Other',
                                        Type = 'Action Request',
                                        Origin ='Internal action',
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        
                                        insert c4;
                                }     

                                insert Feat;

                }
                
if(trigger.new[0].Status=='closed' && (trigger.old[0].Status<>'closed') && (trigger.new[0].Subject=='New hotel FastBooking : Availpro Registration') && (trigger.new[0].Opportunity_Case__c != null))
                {
                
                                List<Case> CaseOpp =new List<Case>();
                                
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, PricebookEntry.Product2.ProductCode, Billing_Type__c, CurrencyIsoCode, UnitPrice,TotalPrice,Number_Of_Revenue_Installments__c, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c1=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        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)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c1);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode != 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        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)*0.6,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                                        
                                        if(OLI.Billing_Type__c=='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c3=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        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)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c3);
                                        }
                                        
                                        if(OLI.Billing_Type__c!='Monthly' && OLI.PricebookEntry.Product2.ProductCode == 'FAST_PRODUCT'){
                                        case c2=new case(
                                        Subject='FastBooking : Invoicing :'+' '+ProductName,
                                        Description ='Please, invoice FastBooking for this product',
                                        OwnerId='00520000000pXcV', 
                                        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)*0.4,
                                        CurrencyIsoCode = OLI.CurrencyIsoCode);
                                        CaseOpp.add(c2);
                                        }
                            }
                            
                            insert CaseOpp;

                }
}
And my test class 
@isTest(SeeAllData=true)
private class TestCreateBillingCaseonCase3 {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity3() {
        // 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='0019E00000TDyso',
                                       Starting_date__c=date.Today(),
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='0039E00000Mtqhs',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 pro = new Product2(Name='training', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro;
        
        Product2 pro2 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Once',CurrencyIsoCode='EUR');
        insert pro2;
        
        Product2 pro3 = new Product2(Name='training', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro3;
        
        Product2 pro4 = new Product2(Name='training', ProductCode='FAST_PRODUCT', isActive=true, Billing_Type__c='Monthly',CurrencyIsoCode='EUR');
        insert pro4;

        PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe;
        
        PricebookEntry pbe2 =new PricebookEntry(unitprice=0.01,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;
        
        PricebookEntry pbe3 =new PricebookEntry(unitprice=0.01,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe3;
        
        PricebookEntry pbe4 =new PricebookEntry(unitprice=0.01,Product2Id=pro4.Id,Pricebook2Id=Test.getStandardPricebookId(),
                                             isActive=true,UseStandardPrice = false);
        insert pbe4;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI;
        
        OpportunityLineItem OLI2 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI2;
        
        OpportunityLineItem OLI3 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe3.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI3;
        
        OpportunityLineItem OLI4 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe4.Id,Number_Of_Revenue_Installments__c=12);     
        insert OLI4;

        Case ca = new Case(Subject='New hotel FastBooking : 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();
    }
    
}
Thanks for your help
Thomas
Alain CabonAlain Cabon
You have used @isTest(SeeAllData=true) so all the data created by your test class are quite useless because you are using the real data and some of them don't have Number_Of_Revenue_Installments__c initialized.

(try: select count(id) from OpportunityLineItem where Number_Of_Revenue_Installments__c = null )

Why do you need  @isTest(SeeAllData=true)?
 
Thomas CailletThomas Caillet
Thanks Alain but I used @isTest(SeeAllData=true) just to test but doesn't work without too.
Alain CabonAlain Cabon
"it doesn't work without too." because your test is not correct (you are using many Ids).

Never use the Ids directly as far as possible.

 OwnerId='00520000000pXcV', // it is an exception if you have this user in each org (sandboxes, production).

 AccountId='0019E00000TDyso',   // bad, that only works in one org (even with @isTest(SeeAllData=true) )

// the following products should be created in the test
(PricebookEntry.Product2.ProductCode = '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2'      
AND PricebookEntry.Product2.ProductCode '15EEC0D0-1591-4F29-BE62-039EBE1EE13D.2' 

In the worse cases, we just use Test.isRunningTest() in the tested class to exclude some lines for the rare cases impossible to test but here all could be tested

Test.isRunningTest(): https://help.salesforce.com/articleView?id=000205831&type=1

 
Thomas CailletThomas Caillet
Yes I used many Ids but all these Ids exist in my production and my sandboxe (except for account Ids but I used sandboxe's accounts)
The only thing is the test don't recognize my field "Number_Of_Revenue_Installments__c" and I don't know why.
Alain CabonAlain Cabon
Number_Of_Revenue_Installments__c not recognize or the test read this field wthout value (null values in your sandbox).

Is it a new field and the visibility is not correct ?

Can you query this field directly like that ?

select count(id) from OpportunityLineItem where Number_Of_Revenue_Installments__c = null

What is the result?
Thomas CailletThomas Caillet
It's not a new field and it's editable for all.
You want that I query in the test class ?
Alain CabonAlain Cabon
select count(id) from OpportunityLineItem where Number_Of_Revenue_Installments__c = null

... but you have never used the query editor of the developer console so it is useless.

You should also post your question and test class here:  https://salesforce.stackexchange.com/

Their developers will not code a solution like here but you will receive many offer constructive criticism about your test class.