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
Saania KhanSaania Khan 

PriceBookEntry records in Apex Test Class

Hi,

I am using the following snippet in a Test Class. When I copy it in Developer Console, it works just fine ! But in the test class, the size of pbe list is zero !
List<PriceBookEntry> pbe = new List<PriceBookEntry>( );
    pbe = [Select Id,Name,PriceBook2ID, Product2Id from PriceBookEntry where ProductCode = 'NRG-HF-89-C0' and CURRENCYISOCODE='USD'];

System.Debug(pbe.size());
Opportunity o= new Opportunity(Name = 'Test Opportunity', StageName = 'Drop In', CloseDate = system.today() + 30);
        insert o;
        Quote q= new Quote(Name = 'Test Quote', Status = 'Draft', OpportunityId = o.Id, Pricebook2Id=pbe[0].PriceBook2Id);
        insert q;
        QuoteLineItem qli= new QuoteLineItem (Quantity=1, QuoteId=q.Id,UnitPrice=100.00, Discount = 50, PriceBookEntryId = pbe[0].Id, Product2Id=pbe[0].Product2Id);
        
        insert qli;
        System.assertNotEquals(null,q); 
        
          
        // Retrieve the create quote line item
        qli = [SELECT Id,TotalPrice,Discount,Discount_Amount__c FROM QuoteLineItem WHERE Id =:qli.Id];  
    
        System.assertEquals(50,qli.Discount_Amount__c);
        
        qli.Discount = 25;
        
        update qli;
        
        // Retrieve the create quote line item
        qli = [SELECT Id,TotalPrice,Discount,Discount_Amount__c FROM QuoteLineItem WHERE Id =:qli.Id];  
    
        System.assertEquals(25,qli.Discount_Amount__c);

delete o;

Thanks !
Best Answer chosen by Saania Khan
kirubakaran viswanathankirubakaran viswanathan
Use @isTest(SeeAllData=true). If not, insert new records from the test class to PBE and query.

All Answers

kirubakaran viswanathankirubakaran viswanathan
Use @isTest(SeeAllData=true). If not, insert new records from the test class to PBE and query.
This was selected as the best answer
Saania KhanSaania Khan
Oh wow ! @isTest(SeeAllData=true) worked ! Thanks a bunch kirubakaran !