+ Start a Discussion
Mont MontMont Mont 

Can't insert OpportunityLineItem in Test method

Hello. I have problems with this fragment in my Test method:

Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        insert pb2;
        
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
         
       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        
        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here
Crush in 'insert item'. Where is a problem? I don't see it.
Best Answer chosen by Mont Mont
Steven NsubugaSteven Nsubuga
An entry in the standard pricebook is required, and you were not inserting the pbe.
 
Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        insert pb2;
        
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
         
        // create standard pricebook entry
        PricebookEntry standardpbe = new PricebookEntry(Pricebook2Id = test.getstandardpricebookid(), Product2Id = prod.Id, UnitPrice = 0.0, IsActive = true);
        insert standardpbe;

       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        insert pbe;               

        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
       item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here

 

All Answers

VahidVahid
Hi,

There is 2 problem:
1. You need standard pricebook which is not available. 
2. You did not insert PricebookEntry (Line # 13). You must insert Pricebook Entry (insert pbe). 

try the following code:

Account acc = new Account(Name ='testing acc');
        insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
        insert prod;
               
        //PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        //insert pb2;
        Id pricebookId = Test.getStandardPricebookId();
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pricebookId, StageName = 'Prospecting');
        insert opport;
         
           PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        insert pbe;
        
        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //Supposed to work

Thanks
Abdul Vahid
iBirds Software Services Pvt. Ltd.
v varaprasadv varaprasad
Hi Mont,

Please check one below code u have to use standard price book in the test class.

 
// This is how we get the Standard PriceBook Id.  Prior to Summer '14, we needed
// to use SeeAllData=true, so this is a big improvement
//Id pricebookId = Test.getStandardPricebookId();


Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        Id pricebookId = Test.getStandardPricebookId();
        
		PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pricebookId.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
		
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pricebookId.Id, StageName = 'Prospecting');
        insert opport;
         
      
        
        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
        item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For  Support: varaprasad4sfdc@gmail.com


 
Steven NsubugaSteven Nsubuga
An entry in the standard pricebook is required, and you were not inserting the pbe.
 
Account acc = new Account(Name ='testing acc');
		insert acc;
        
        Product2 prod = new Product2(Name = 'AutoBot', IsActive = true);
		insert prod;
               
        PriceBook2 pb2 = new PriceBook2(Name = 'test', IsActive = true);
        insert pb2;
        
        Opportunity opport = new Opportunity(Name = 'new opp', CloseDate = date.today(), AccountId= acc.Id, Pricebook2Id = pb2.Id, StageName = 'Prospecting');
        insert opport;
         
        // create standard pricebook entry
        PricebookEntry standardpbe = new PricebookEntry(Pricebook2Id = test.getstandardpricebookid(), Product2Id = prod.Id, UnitPrice = 0.0, IsActive = true);
        insert standardpbe;

       	PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb2.Id, Product2Id = prod.Id, UnitPrice = 10.0, IsActive = true);
        insert pbe;               

        OpportunityLineItem item = new OpportunityLineItem();
        item.OpportunityId = opport.Id;
        item.Product2Id = prod.Id;
       item.PricebookEntryId = pbe.Id;
        item.Quantity = 2;
        item.TotalPrice = item.Quantity * pbe.UnitPrice;
        
        insert item;  //crush here

 
This was selected as the best answer
Mont MontMont Mont
Thank you, guys :)