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
emuelasemuelas 

Create quotelineitem for test class

Hi ,

 

I have a test class where i need to be able to create a quotelineitem.

 

Can someone please help me with the code to create a quotelineitem for a test class?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
kpeterskpeters

Here's an example of creating an OpportunityLineItem in a test.  You should be able to do the exact same thing but with a QuoteLineItem:

 

 

Product2 p = new product2(name='unittest');
p.Family = 'License';
insert p;
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
PricebookEntry pbe = new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0,isActive=true);
insert pbe;
OpportunityLineItem oli = new OpportunityLineItem(PricebookEntryId=pbe.Id,OpportunityId=o1.Id,Quantity=10,TotalPrice=100);

 

This is assumming you've already built an Opportunity called o1.

 

Hope this helps.

 

All Answers

kpeterskpeters

Here's an example of creating an OpportunityLineItem in a test.  You should be able to do the exact same thing but with a QuoteLineItem:

 

 

Product2 p = new product2(name='unittest');
p.Family = 'License';
insert p;
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
PricebookEntry pbe = new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id,unitprice=1.0,isActive=true);
insert pbe;
OpportunityLineItem oli = new OpportunityLineItem(PricebookEntryId=pbe.Id,OpportunityId=o1.Id,Quantity=10,TotalPrice=100);

 

This is assumming you've already built an Opportunity called o1.

 

Hope this helps.

 

This was selected as the best answer
emuelasemuelas

Hi kpeters,

 

Actually i used this and created a quote ,so the quote line item should automatically be created...but thats not happening,

 

I tried  replacing opportunity product with quotelineitem values but that is erroring out

kpeterskpeters

Actually i used this and created a quote ,so the quote line item should automatically be created...but thats not happening,


I was assumming you wanted to add a QLI in your test method after creating the quote.  If you want the QLI to be automatically added to the quote based on the OLI in the opportunity, the quote has to be synced with the opportunity.  AFAIK, there is no way to sync a quote to an opportunity in apex.

 


I tried  replacing opportunity product with quotelineitem values but that is erroring out


What error are you getting?

emuelasemuelas

Hi ,

 

thank you so much!

 

Finally figured out the error....

 

the quote was not assigned any pricebook id...so changed that......

 

 

quote q=new quote(name='myquote',opportunityid=opp.id,pricebook2id=pb.id);
 
 insert q;
 

quoteLineItem i = new quotelineitem(); 

i.quoteid      = q.id; 

i.pricebookentryid    = pbe.id; 

i.quantity            = 1; 

i.unitprice           = 1; 

   

Database.insert(i);