+ Start a Discussion
Sylvain@RibbonFishSylvain@RibbonFish 

Product with Pricebook2 with pricebookEntry

Hi,

This code was working previously since one month I can't run it:

Product2 pdt2 = new Product2(Name='Test',ProductCode = '222');
        Product2 pdt3 = new Product2(Name='Test',ProductCode = '222');

  
        insert pdt2;
        insert pdt3;

        Pricebook2 pBook = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
        PricebookEntry pBookEntry = new PricebookEntry(UnitPrice=35,Pricebook2Id=pBook.Id,Product2Id=pdt2.Id,IsActive=true,UseStandardPrice=false);
        insert pBookEntry;

 The error:

 

Products do not have a standard price.

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Sylvain@RibbonFishSylvain@RibbonFish
any idea ? any help ?

All Answers

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

You might have deactivated the standard price in Pricebook, that using the product.

Sylvain@RibbonFishSylvain@RibbonFish
Hi,
I did check that I got only one PB where is corresponding to the Standard PB and is active.
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

If you have not any of your custom pricebooks created and products assigned to that pricebook, it will not show for you. check again or explain your requirement in detail.

 

Sylvain@RibbonFishSylvain@RibbonFish
Hi,
My requirement is to create a PricebookEntry in my test unit with a product.
So that's what to code in my first code was doing but not anymore.
Thanks
Sylvain@RibbonFishSylvain@RibbonFish

Ok maybe I was not really clear sorry I was in rush.

So I do need to create a priceBookEntry with a product in a test method (test unit, test file).

You can see my first post which create a product then query the pricebook2 table and create a priceBookEntry object to be able to create a sale opportunity after.

I'm getting the following error: 

"Products do not have a standard price."

So if someone can help me please.

Thanks

Sylvain@RibbonFishSylvain@RibbonFish
any idea ? any help ?
This was selected as the best answer
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

The following the procedure to insert records to test. Try this way,

 

Product2 p = new product2(name='demo');
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);
 
 
 

Hope so this helps you...! 

Please mark this answer a Solution and please give kudos by clicking on the star icon, if you found this answer as helpful.