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
masthan khanmasthan khan 

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

Hai,
       When inserting arecord it is gthrowing following error :
 
System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: [];
public class DML_Practice_Usercase4 {
    public List<PriceBookEntry> pbe{set;get;}
    public List<product2> pb{set;get;}
    public list<Pricebook2> pb2{set;get;}
    public DML_Practice_Usercase4(){
       pbe=new list<PriceBookEntry>();
product2  pb=[select id from product2 where name='salesforce' ];
Pricebook2 pb2=[SELECT id FROM Pricebook2 WHERE Name = 'IndianPricebook'];
        PriceBookEntry p1=new PriceBookEntry();
        p1.Product2id=pb.id;
        p1.Pricebook2ID=pb2.id;
        p1.unitprice=10000;
         pbe.add(p1);  
        insert pbe;   
    }
}
Best Answer chosen by masthan khan
Steven NsubugaSteven Nsubuga
An entry in the standard pricebook is required.
public class DML_Practice_Usercase4 {
    public List<PriceBookEntry> pbe{set;get;}
    public List<product2> pb{set;get;}
    public list<Pricebook2> pb2{set;get;}
    public DML_Practice_Usercase4(){
       pbe=new list<PriceBookEntry>();
product2  pb=[select id from product2 where name='salesforce' ];

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true]; 
PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = pb.Id, UnitPrice = 0, IsActive = true); 
insert standardPBE;

Pricebook2 pb2=[SELECT id FROM Pricebook2 WHERE Name = 'IndianPricebook'];
        PriceBookEntry p1=new PriceBookEntry();
        p1.Product2id=pb.id;
        p1.Pricebook2ID=pb2.id;
        p1.unitprice=10000;
         pbe.add(p1);  
        insert pbe;   
    }
}

 

All Answers

Steven NsubugaSteven Nsubuga
An entry in the standard pricebook is required.
public class DML_Practice_Usercase4 {
    public List<PriceBookEntry> pbe{set;get;}
    public List<product2> pb{set;get;}
    public list<Pricebook2> pb2{set;get;}
    public DML_Practice_Usercase4(){
       pbe=new list<PriceBookEntry>();
product2  pb=[select id from product2 where name='salesforce' ];

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true]; 
PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = pb.Id, UnitPrice = 0, IsActive = true); 
insert standardPBE;

Pricebook2 pb2=[SELECT id FROM Pricebook2 WHERE Name = 'IndianPricebook'];
        PriceBookEntry p1=new PriceBookEntry();
        p1.Product2id=pb.id;
        p1.Pricebook2ID=pb2.id;
        p1.unitprice=10000;
         pbe.add(p1);  
        insert pbe;   
    }
}

 
This was selected as the best answer
masthan khanmasthan khan
Hai 
Steven Nsubuga,
Its working but what is the relation between isStandard and is Isactive.. 
why are we inserting record twice.
 Regards
Masthan.
Steven NsubugaSteven Nsubuga
Hi Masthan, there must be at least 1 Pricebook that is designated as Standard (isStandard ). You can also create many other Pricebooks, and if you nolonger need one you can deactivate it (isActive).
There must be an entry in the Standard Pricebook for each Product in Salesforce. This is a Salesforce rule. So whenever you create a new product and a new pricebook, that new product must exist in the standard pricebook before it exists in your new pricebook.