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
Force2b_MikeForce2b_Mike 

Possible bug in API 24 (Spring '12) Unit Test with Pricebook

I've noticed that if you're writing a unit test using v24 of the API without the @SeeAllData=true annotation, there seems to be a catch-22 type problem with the Standard Pricebook.

 

The following unit test code will return an error because the Unit Test does not see the Standard Pricebook: 

string standardPB = [Select Id from Pricebook2 where IsStandard=true limit 1].Id;

 

If you try to create a new Standard Pricebook with the following, that code cannot be saved since the IsStandard field is not updatable (error = "Field is not writeable: Pricebook2.IsStandard"):

Pricebook2 pb = new Pricebook2(Name='Standard Pricebook', IsStandard = true);
insert(pb);

 

 

The result is that you simply can't write a unit test that needs to be able to create an Opportunity or Quote with Products in v24 without using @SeeAllData=true. Obviously not best practice, but I can't find a work-around.

 

 

Best Regards,

 

Mike

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

People have already asked this question... Basically, the answer is as follows: http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit. In summary: SeeAllData will not be deprecated, so feel free to use it; yes, they know that unit testing is broken; yes, they plan to fix this and other related "sandboxed-test-unit" issues in the future, so use a workaround until then.

All Answers

steve456steve456

Change the version to 23 you are good to go

sfdcfoxsfdcfox

People have already asked this question... Basically, the answer is as follows: http://stackoverflow.com/questions/9164986/how-do-i-avoid-standard-price-not-defined-when-unit-testing-an-opportunitylineit. In summary: SeeAllData will not be deprecated, so feel free to use it; yes, they know that unit testing is broken; yes, they plan to fix this and other related "sandboxed-test-unit" issues in the future, so use a workaround until then.

This was selected as the best answer
Force2b_MikeForce2b_Mike

Yes, changing API versions works as well; basically the same as using SeeAllData=true. 

 

The issue is that it's a bug and I'm hoping Salesforce.com will fix.

 

 

Best Regards,

 

Mike

Force2b_MikeForce2b_Mike

Thanks. Guess I need to spend more time over at Stackoverflow.com.