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
kishore64kishore64 

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

Hi all,

Here i am writng the test class like,


@isTest(seeAllData=true)
public class testSyncQuotaLineItemsCls{
public static testMethod void testSyncQuotaLineItemsCls(){

             Contact con = New Contact();
             con.LastName='test';
             insert con;

            Opportunity opp = new Opportunity();
            opp.Name='test';
            opp.CloseDate=Date.Today();
            opp.StageName='Prospecting';
            opp.Contact_del__c=con.id;
            insert opp;
           
             Quote qta = new Quote();
             qta.Name='test';
             qta.opportunityid=opp.id;
             qta.Address_Information__c='Bangalore';
             insert qta;
           
             Sales_Order_LineItem__c soli = New Sales_Order_LineItem__c();
             soli.Name='test';
             insert soli;
           
             Product2 pr = New Product2();
             pr.Name='test';
             pr.IsActive=true;
             insert pr;
           
             PriceBook2 pb2 = New PriceBook2();
             pb2.Name = 'test';
             pb2.IsActive=true;
            // pb2.IsStandard=true;
             insert pb2;
            Pricebook2 p =[Select p.Id, p.Name from Pricebook2 p where isActive = true AND isStandard=true Limit 1];
         
      
             PricebookEntry pb = New PricebookEntry();
                pb.Product2Id = pr.Id;
                pb.PriceBook2Id = pb2.id;
                pb.UseStandardPrice = true;
                pb.UnitPrice=35;
                pb.isActive=true;
                insert pb;                      
   PricebookEntry pb1 = [select id,name,usestandardprice from PricebookEntry where usestandardprice=true];
 

             QuoteLineItem qli = New QuoteLineItem();
             qli.Product2Id=pr.id;
             qli.UnitPrice=12;
             qli.Quantity=5;
             qli.QuoteId=qta.id;
             qli.UnitPrice=35;
            // qli.ListPrice=410;
             //qli.Subtotal=123;
             //qli.TotalPrice=410;
             qli.PricebookEntryId=pb.id;
             insert qli;
       
           string qtaid = qta.id;
   
     SyncQuotaLineItemsCls sqli = New SyncQuotaLineItemsCls();
        SyncQuotaLineItemsCls.syncQLItoSLI(qtaid);

     }
}

Error is:  System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

Here where i did mistake i don't know . please help me out .

Advance Thank you.
AshwaniAshwani
Hi,

Apex test class doesn't suport creating pricebook.

In summer '14 there is new method which can be used to create pricebook for you:-

Test.getStandardPricebookId();

Look at following example:

@isTest
                                    
public class PriceBookTest {
    // Utility method that can be called by Apex tests to create price book entries.
    static testmethod void addPricebookEntries() {
        // First, set up test price book entries.
        // Insert a test product.
        Product2 prod = new Product2(Name = 'Laptop X200', 
            Family = 'Hardware');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        // Create a custom price book
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        // 2. Insert a price book entry with a custom price.
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        // Next, perform some tests with your test price book entries.
    }
}

Also, you don't need (SeeAllData=true) then.
AshlekhAshlekh
Hi,

In summer 14 a new feature is added, you can get the id of standard pricebook by Test.getStandardPricebookId() this method will give you the id of standard price book.

You don't need to create any price book and use existing standard price book.

@isTest(seeAllData=true)
public class testSyncQuotaLineItemsCls{
public static testMethod void testSyncQuotaLineItemsCls(){

             Contact con = New Contact();
             con.LastName='test';
             insert con;

            Opportunity opp = new Opportunity();
            opp.Name='test';
            opp.CloseDate=Date.Today();
            opp.StageName='Prospecting';
            opp.Contact_del__c=con.id;
            insert opp;
           
             Quote qta = new Quote();
             qta.Name='test';
             qta.opportunityid=opp.id;
             qta.Address_Information__c='Bangalore';
             insert qta;
           
             Sales_Order_LineItem__c soli = New Sales_Order_LineItem__c();
             soli.Name='test';
             insert soli;
           
             Product2 pr = New Product2();
             pr.Name='test';
             pr.IsActive=true;
             insert pr;
           
          /*   PriceBook2 pb2 = New PriceBook2();
               pb2.Name = 'test';
               pb2.IsActive=true;
              //pb2.IsStandard=true;
               insert pb2;
           Pricebook2 p =[Select p.Id, p.Name from Pricebook2 p where isActive = true AND isStandard=true Limit 1];
          */
      
             PricebookEntry pb = New PricebookEntry();
                pb.Product2Id = pr.Id;
                pb.PriceBook2Id = Test.getStandardPricebookId();
                pb.UseStandardPrice = true;
                //pb.UnitPrice=35;//when we use standarprice book =true thenunit price is readonly field
                pb.isActive=true;
                insert pb;                      
          //PricebookEntry pb1 = [select id,name,usestandardprice from PricebookEntry where usestandardprice=true];
 

             QuoteLineItem qli = New QuoteLineItem();
             //qli.Product2Id=pr.id;
             qli.UnitPrice=12;
             qli.Quantity=5;
             qli.QuoteId=qta.id;
             //qli.UnitPrice=35;
            // qli.ListPrice=410;
             //qli.Subtotal=123;
             //qli.TotalPrice=410;
             qli.PricebookEntryId=pb.id;
             insert qli;
       
           string qtaid = qta.id;
   
     SyncQuotaLineItemsCls sqli = New SyncQuotaLineItemsCls();
        SyncQuotaLineItemsCls.syncQLItoSLI(qtaid);

     }
}

IF it helps you than please mark it as a solution and like it. ENJOY APEX
Deepak Kumar ShyoranDeepak Kumar Shyoran
Don't put multiple question for same problem as you've listed the Same question here
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AeOcIAK
kishore64kishore64
Hi Ashlekh Gera (D-Horse),

        I am doing in your way  but it shows an exception because of unit price(43rd line)  i uncommented that one. Now again i am executing this test class it shows some other exception i,e.  

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []

Then what can i do, i want to chnage any other thing please let me know......