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 

Add opportunity line items with ProductCode in test trigger

Hello everybody,

I try in test class to add an opportunity line item WITH a product who have a product code.
But when I add : system.debug (OLI.PricebookEntry.Product2.ProductCode), I have a null value. Could you tell me why ?

Here is my test class : 
@isTest(SeeAllData=true)
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='001w000001eDff7',
                                       Starting_date__c=date.Today(),
                                       CurrencyIsoCode='EUR',
                                       CloseDate=System.today().addMonths(1)
                                       );
        insert opp;           
        
        Quote QUOT = new Quote (Name='TEST',BillingCountry = 'France' ,BillingPostalCode = '75009' ,ContactId='003w000001sI2o5',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='Google', isActive=true, ProductCode = '4263E3EC-AB8F-4B3B-B004-F60E1FF15268.2');
        insert pro;

        Product2 pro2 = new Product2(Name='training', isActive=true, Billing_Type__c='Once');
        insert pro2;
        
        Product2 pro3 = new Product2(Name='test', isActive=true, Billing_Type__c='Monthly');
        insert pro3;
        
        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;
        
        Existing_feature__c exf = new Existing_feature__c (Name='TEST', FeatureId__c='123456', Product__c= pro2.Id);
        insert exf;
        
        OpportunityLineItem OLI = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe2.Id,ServiceDate=System.TODAY());     
        insert OLI;
        
        OpportunityLineItem OLI2 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe.Id,ServiceDate=System.TODAY());     
        insert OLI2;
        
        OpportunityLineItem OLI3 = new OpportunityLineItem (TotalPrice=240,opportunityId = opp.Id,Quantity=1,PricebookEntryId=pbe3.Id,ServiceDate=System.TODAY());     
        insert OLI3;

        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();
    }
    
}
Thanks
Thomas