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
Ryno LourensRyno Lourens 

Creating Test Data for Opportunties

Hi everyone,

I do a lot of work that involves the Opportunity object, along with the attached Price Book, Product, and Opportunity Product objects.

Because these objects and their relationships can get kind of complicated (especially creating standard and custom Price Book entries for each Product before creating an Opportunity Product), I want to create an Apex Class that will generate this data when needed. That way I can call the necessary data whenever I run a new test, instead of adding that code into each new Apex test class I create.


This is what I have so far, but I'm unsure if I'm on the right track:

@isTest
public class OpportunityDataGenerator {
    
    // create test data
    @testSetup public static void createOpportunityData() {
        // create standard price book
        Pricebook2 stdPricebook = new Pricebook2(
            Name = 'Standard Price Book', 
            IsActive = true
        );
        insert stdPricebook;
        
        // create custom price book
        Pricebook2 customPricebook = new Pricebook2(
            Name = 'Custom Price Book', 
            IsActive = true
        );
        insert customPricebook;
        
        // create product with standard price book and custom price book entries
        Product2 product = new Product2(
            Name = 'Test Product',
            ProductCode = 'TEST001',
            IsActive = true
        );
        insert product;
        
        // add standard price book entry for the product
        PricebookEntry stdPricebookEntry = new PricebookEntry(
            Pricebook2Id = stdPricebook.Id,
            Product2Id = product.Id,
            UnitPrice = 100.0,
            IsActive = true
        );
        insert stdPricebookEntry;
        
        // add custom price book entry for the product
        PricebookEntry customPricebookEntry = new PricebookEntry(
            Pricebook2Id = customPricebook.Id,
            Product2Id = product.Id,
            UnitPrice = 150.0,
            IsActive = true
        );
        insert customPricebookEntry;
        
        // create account with Client_Type__c field
        Account account = new Account(
            Name = 'Test Account',
            Client_Type__c = 'Associate'
        );
        insert account;
        
        // create opportunity related to the account
        Opportunity opportunity = new Opportunity(
            Name = 'Test Opportunity',
            StageName = 'Prospecting',
            CloseDate = Date.today(),
            AccountId = account.Id
        );
        insert opportunity;
        
        // create opportunity product related to the opportunity and the product
        OpportunityLineItem opportunityProduct = new OpportunityLineItem(
            OpportunityId = opportunity.Id,
            PricebookEntryId = customPricebookEntry.Id,
            Quantity = 10,
            UnitPrice = customPricebookEntry.UnitPrice
        );
        insert opportunityProduct;
    }
}
Prateek Prasoon 25Prateek Prasoon 25
Answer :-
The test class provided looks good and creates the necessary data to test functionality related to the Opportunity, Pricebook2, Product2, and OpportunityLineItem objects. However, there are a few minor changes that can be made for optimization and readability:
Add comments: While the current code has comments for each section, it's best practice to add comments for each line to explain what the code is doing.
Use constants: To avoid hard-coding IDs or values, it's best to use constants that are defined at the beginning of the class. This makes it easier to update values if needed and reduces the likelihood of errors.
Use try-catch blocks: To handle any exceptions that may occur during data creation, it's best to wrap each insert statement in a try-catch block.
Here's an updated version of the class that implements these changes:
 
Text
      
    
    
    
      @isTest
public class OpportunityDataGenerator {
    // Constants
    private static final String STD_PRICEBOOK_NAME = 'Standard Price Book';
    private static final String CUSTOM_PRICEBOOK_NAME = 'Custom Price Book';
    private static final String PRODUCT_NAME = 'Test Product';
    private static final String PRODUCT_CODE = 'TEST001';
    private static final String ACCOUNT_NAME = 'Test Account';
    private static final String CLIENT_TYPE = 'Associate';
    private static final String OPPORTUNITY_NAME = 'Test Opportunity';
    private static final String STAGE_NAME = 'Prospecting';
    private static final Integer QUANTITY = 10;
    private static final Decimal UNIT_PRICE = 150.0;
    
    // create test data
    @testSetup 
    public static void createOpportunityData() {
        try {
            // create standard price book
            Pricebook2 stdPricebook = new Pricebook2(
                Name = STD_PRICEBOOK_NAME, 
                IsActive = true
            );
            insert stdPricebook;
            // create custom price book
            Pricebook2 customPricebook = new Pricebook2(
                Name = CUSTOM_PRICEBOOK_NAME, 
                IsActive = true
            );
            insert customPricebook;
            // create product with standard price book and custom price book entries
            Product2 product = new Product2(
                Name = PRODUCT_NAME,
                ProductCode = PRODUCT_CODE,
                IsActive = true
            );
            insert product;
            // add standard price book entry for the product
            PricebookEntry stdPricebookEntry = new PricebookEntry(
                Pricebook2Id = stdPricebook.Id,
                Product2Id = product.Id,
                UnitPrice = 100.0,
                IsActive = true
            );
            insert stdPricebookEntry;
            // add custom price book entry for the product
            PricebookEntry customPricebookEntry = new PricebookEntry(
                Pricebook2Id = customPricebook.Id,
                Product2Id = product.Id,
                UnitPrice = UNIT_PRICE,
                IsActive = true
            );
            insert customPricebookEntry;
            // create account with Client_Type__c field
            Account account = new Account(
                Name = ACCOUNT_NAME,
                Client_Type__c = CLIENT_TYPE
            );
            insert account;
            // create opportunity related to the account
            Opportunity opportunity = new Opportunity(
                Name = OPPORTUNITY_NAME,
                StageName = STAGE_NAME,
                CloseDate = Date.today(),
                AccountId = account.Id
            );
            insert opportunity;
            // create opportunity product related to the opportunity and the product
            OpportunityLineItem opportunityProduct = new OpportunityLineItem(
                OpportunityId = opportunity.Id,
                PricebookEntryId = customPricebookEntry.Id,
                Quantity = QUANTITY,
                UnitPrice = UNIT_PRICE
            );
            insert opportunityProduct;
        } catch (Exception

If you find my answer helpful, please mark it as the best answer. Thanks!