You need to sign in to do that
Don't have an account?
Eldon
Cant access pricebook2.isstandard from my test class
Hi
I am trying to query my standard pricebook in my apex method by following code,
PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true];
Im getting the result in my application but when i write test class and call the method containing that code im getting the following error,
System.QueryException: List has no rows for assignment to SObject.
finally i did my test class using seealldata=true and im able to get the results
What is the reason for this?
I am trying to query my standard pricebook in my apex method by following code,
PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true];
Im getting the result in my application but when i write test class and call the method containing that code im getting the following error,
System.QueryException: List has no rows for assignment to SObject.
finally i did my test class using seealldata=true and im able to get the results
What is the reason for this?
1.We no need to use (seeallData=true) after summer 14 release.
2.Make sure that your test class API version is latest(37).
Use below code as a reference
for more info
https://releasenotes.docs.salesforce.com/en-us/summer14/release-notes/rn_apex_price_books_in_tests.htm
If it helps make it as best answer.
Thanks.
I am using Test.getStandardPricebookId(); in my test class.
The problem is when i call my apex method from my test class the following code in my apex method is not working.
PriceBook2 PB=[select id,Name from Pricebook2 where IsStandard = true];
We can use test.getStandardPricebookId(); only in our test class. My req is that i want to use the standard pricebook id in my apex method. Hope you understood the problem.
Your query may returns more than one row.Try with limit like this.
Thanks.
System.QueryException: List has no rows for assignment to SObject.
What i did is i used a custom pricebook in my app. If you want to see the std PB you will have to use seealldata=true in your test.
Regards
Yes Eldon, I agree with you.
But in Summer'14 Release Salesforce stated that 'You can create price book entries for standard and custom price books in Apex tests.' If we cannot access standard pricebook without seealldata = true, then what is the purpose of adding this feature. May be Salesforce team have to made some amendments to this feature, otherwsie they should convey the information properly in Release Notes.
Anaways, Thank you for your suggestion.
If someone who does not wnat to put seealldata = true, they can try using Test.isRunningTest() if block and read the normal pricebook values just to cover the code.
Thak you.
You can fetch Standard Pricebook in test class by
this way-:
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi