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?
 
@isTest
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 = a.id, closeDate = date.today(), 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 = p.id, UnitPrice = 100, IsActive = true);
       insert pe;

       oli = new OpportunityLineItem(OpportunityId = o.id, Quantity = 1, PricebookEntryId = pe.id, totalPrice = 200);
       insert oli;

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

       qli = new QuoteLineItem(QuoteId = q.id, PriceBookentryId = pe.Id, Quantity = 1, UnitPrice = 1);
       insert qli; 
       System.debug(q);
       System.debug(qli);
       System.debug(q.QuoteLineItems);
   }

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]
system.debug(quoteList[0].QuoteLineItems);

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

Thanks!
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 Id=:q.id];
System.debug(q.QuoteLineItems);

Thank You!
Rajendra