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
Gage Staruch 9Gage Staruch 9 

No standard price defined for this product error

Hi guys!

I am getting the below error on this test. class:

Error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

 

Code:

@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        Account acc = new Account();
        acc.name= 'test';
        insert acc;
        
        Product2 newProd = new Product2 (Name = 'Test Product', ProductLine__c ='Test Product Line');
		insert newProd;
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;    
        
    	Pricebook2 customPB = new Pricebook2(Name='Test Product', isActive=true);
        insert customPB;
        
        PriceBookEntry pbEntry = new PriceBookEntry(UnitPrice = 100.00, PriceBook2Id = customPB.Id, Product2Id = newProd.Id, IsActive = true, UseStandardPrice = false);
		insert pbEntry ;

		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select custombox__c from Account limit 1];
        Test.stopTest();
         }
}
Can anyone help with this? Any hints would be appreciated! 
Sachin HoodaSachin Hooda
Hi Gage,
The issue is because you haven't added your product to a standard priceboook. So, you need to create a PricebookEntry for the product with the standard pricebook.
Since its a test class you can get Id of the standard pb as,
Id standPBId = Test.getStandardPricebookId();
Once you get the Id you need to create a pricebook entry for the standard price,
PricebookEntry pbe = new PricebookEntry();
pbe.Pricebook2Id = standPBId;
pbe.Product2Id = newProd.Id;
pbe.UnitPrice = 100;
pbe.isActive = true;

INSERT pbe;
This should fix your issue.
 
Le NguyenLe Nguyen
The error is because a product in Salesforce has to have a standard price which is StandardPriceBook in this case.  Rather than initializing a Pricebook2, you can just use the getStandardPricebookId from Test class.
 
@isTest
private class ActiveAccountTest{
    static testmethod void validateActiveAccount()
    {
        Account acc = new Account();
        acc.name= 'test';
        insert acc;
        
        Product2 newProd = new Product2 (Name = 'Test Product', ProductLine__c ='Test Product Line');
		insert newProd;
        
        Opportunity oppObj = new Opportunity(Name = 'TestOpp',AccountID = acc.Id,Amount = 2000,CloseDate=Date.today(),StageName='A - Purchase / Close Won', MarketingGeneratedType__c = 'Sales');
		insert oppObj;    
        
Id starndardPriceID = Test.getStandardPricebookId();
        
        PriceBookEntry pbEntry = new PriceBookEntry(UnitPrice = 100.00, PriceBook2Id = starndardPriceID, Product2Id = newProd.Id, IsActive = true, UseStandardPrice = false);
		insert pbEntry ;


		OpportunityLineItem oppLine = new OpportunityLineItem(pricebookentryid=pbEntry.Id,TotalPrice=2000,Quantity = 1,OpportunityID = oppObj.Id);
		insert oppLine;
        
        Test.startTest();
            Account acct4 = [Select custombox__c from Account limit 1];
        Test.stopTest();
         }
}
Le
litt mannlitt mann
quickpayportal gives patients a more flexible and efficient way to pay all their medical bills which also means that patients can pay their bills at least 10 times faster than the conventional process.  https://quickpayportal.red