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
Jacob RiffJacob Riff 

Creating QuoteLineItem using Apex

For some reason this fails - q.QuoteLineItems is null. Can someone tell me why?
private class QuoteTest {

   static Account A;
   static Opportunity o;
   static Quote q;
   static Product2 p;
   static PriceBookEntry pe;
   static OpportunityLineItem oli;
   static QuoteLineItem qli;

   static {
       a = new Account(name = 'Test account', Jurisdiction__c = 'Denmark', currencyIsoCode = 'DKK');
       insert a;

       o = new Opportunity(name = 'Test opportunity', AccountId =, closeDate =, stageName = 'Demonstration Scheduled');
       insert o;

       p = new Product2(name = 'Test product', type__c = 'Subscription', limits__c='a;b\nc;d');
       insert p;

       Id pbId = Test.getStandardPricebookId();

       pe = new PriceBookEntry(PriceBook2Id = pbId, Product2Id =, UnitPrice = 100, IsActive = true);
       insert pe;

       oli = new OpportunityLineItem(OpportunityId =, Quantity = 1, PricebookEntryId =, totalPrice = 200);
       insert oli;

       q = new Quote(name = 'Test quote', OpportunityId =, PriceBook2Id = pbId);
       insert q;

       qli = new QuoteLineItem(QuoteId =, PriceBookentryId = pe.Id, Quantity = 1, UnitPrice = 1);
       insert qli; 

User-added image
Shrikant BagalShrikant Bagal
it is not an issue as you are inserting "QuoteLineItem" and "Quote" in the same class so you have to query on "Quote":

Please try the following query:
List<Quote> quoteList = [SELECT Id, (SELECT Id FROM QuoteLineItems) FROM Quote WHERE Id=: q.Id]

Shrikant BagalShrikant Bagal
Please mark as best solution so it will help who will face the same issue in future.

Rajendra RathoreRajendra Rathore
Hi Jacob,

Replace line no 37 "System.debug(q.QuoteLineItems);" with that code:
​q = [select Id,Name,(select id,,QuoteId,PriceBookentryId,Quantity,UnitPrice FROMQuoteLineItems) From Quote WHERE];

Thank You!