PDI · Question #137
A developer wants to get access to the standard price book in the org while writing a test class that covers an OpportunityLineItem trigger. Which method allows access to the price book?
The correct answer is C. Use Test.getStandardPricebookid ( ) to get the standard price book ID. To access the ID of the standard price book within a test class in Salesforce, developers should utilize the Test.getStandardPricebookId() method.
Question
A developer wants to get access to the standard price book in the org while writing a test class that covers an OpportunityLineItem trigger. Which method allows access to the price book?
Options
- AUse Test.loadData ( )and a static resource to load a standard price book
- BUse @TestVisible to allow the test method to see the standard price book.
- CUse Test.getStandardPricebookid ( ) to get the standard price book ID.
- DUse @IsTest (SeeAllData=True) and delete the existing standard price book
How the community answered
(63 responses)- A2% (1)
- B6% (4)
- C89% (56)
- D3% (2)
Why each option
To access the ID of the standard price book within a test class in Salesforce, developers should utilize the `Test.getStandardPricebookId()` method.
While `Test.loadData()` can load data from static resources, it is not used to access standard Salesforce setup data like the standard price book ID directly.
`@TestVisible` is used to allow test methods to access private or protected members of a class, not to retrieve standard Salesforce IDs like the standard price book ID.
The `Test.getStandardPricebookId()` method is specifically designed to retrieve the ID of the standard price book in a test context, making it the most direct and reliable approach.
`@IsTest(SeeAllData=True)` exposes all org data, but it is generally discouraged, and deleting the standard price book is not a valid or safe approach to accessing its ID in a test.
Concept tested: Apex testing standard price book
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm#apex_System_Test_getStandardPricebookId
Topics
Community Discussion
No community discussion yet for this question.