I have been building test classes and coping parts from existing classes and reusing them in my new class. I want to create two pricebook entries one in the standard pricebook and one in a custom pricebook. I figure I can insert both at the same time by creating the record in list and then inserting the list. However, I get this error when I try to compile the class in Sublime.

Invalid field initializer: pbeStandard.Pricebook2Id (Line: 43, Column: 4)

What am I doing wrong to add the pricebookEntry for the test class?

example test class:

List<PricebookEntry> pbeL = new List<PricebookEntry>();

    // Create A Pricebook

    Pricebook2  pb = new Pricebook2();
        pb.Name = 'NPD Price Book';
        pb.IsActive = true;
        pb.CurrencISOCode = 'USD';
        pb.Description = 'Custom Pricebook';
    insert pb;

    // Create Product

    Product2 prod = new Product2();
        prod.Name = 'Test Product';
        prod.ProductCode = 'Prod Code';
        prod.IsActive = true;
        prod.Product_Type__c = 'Subscription';
        prod.Product_Launch_Date__c = System.today();
        prod.ProductCode = 'NPD9999999';
        prod.Family = 'NPD Solutions';
        prod.CurrencyISOCode = 'USD';
        prod.Product_Identifier__c = 'Test Product';
    insert prod;

    // Get Standard Pricebook 
    Id pbstd = Test.getStandardPricebookId();

    // Create Pricebook Entries
    PricebookEntry pbeStanadrd = new PricebookEntry(
        pbeStandard.Pricebook2Id = pbstd,
        pbeStandard.Product2Id = prod.Id,
        pbeStandard.UnitPrice = 100.00,
        pbeStandard.IsActive = true,
        pbeStandard.CurrencyISOCode = 'USD');

    PricebookEntry pbeC = new PricebookEntry(
        pbeC.Pricebook2Id = pb.Id,
        pbeC.Product2Id = prod.Id,
        pbeC.UnitPrice = 100.00,
        pbeC.IsActive = true,
        pbeC.CurrencyISOCode = 'USD');

    insert pbeL;

1 Answer 1


You need to remove pbeStandard from Initilization of PricebookEntry object.

PricebookEntry pbeStanadrd = new PricebookEntry(
Pricebook2Id = pbstd,
Product2Id = prod.Id,
UnitPrice = 100.00,
IsActive = true,
CurrencyISOCode = 'USD');


We can initialize objects in two ways,

1. Account objA = new Account(Name='TestA'); // Fields definition Inside constructor

2. Account objA = new Account();
// Now define all the fields one by one
  • 1
    Ohhhhhh, that's it. I had been inserting them one at a time like with the product in my example, however, doing multiple you don't need to do that. Thanks. Oct 10, 2016 at 20:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .