Friday, October 26, 2012

Create Product record with Standard Price in APEX

Post API 24, we have to create all our data for test classes until we set SeeAllData to true. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_data_access.htm

However you cannot create OLI or PricebookEntry without setting SeeAllData since you cannot create a default Pricebook using custom code.



 Pricebook2 pb = [select Id, IsActive from PriceBook2 where IsStandard=True];

 Product2 p1 = new Product2(Name='Prod 1', Family='Container', Description='Prod 1 Description');
 insert p1;
           
 // Create a pricebook entry
 PricebookEntry pbe = new PricebookEntry (Pricebook2Id=pb.id, Product2Id=p1.id, IsActive=true, UnitPrice=100.0);
 insert pbe;

 

No comments:

Post a Comment