I am trying to develop a test class for my apex trigger for converting opportunity products to assets when the opportunity is closed/won. My class is like so:


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

trigger OpportunityAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
    if(o.isWon==true && o.HasOpportunityLineItem==true){
        String opptyId = o.Id;
        OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, Product2.Name,
                                     Product2.Family, PricebookEntry.Product2.Name
                                     From OpportunityLineItem 
                                     WHERE OpportunityId=:opptyId];
        Asset[] ast = new Asset[]{};
        Asset a = new Asset();
        for(OpportunityLineItem ol: OLI){
        if(ol.Product2.Family=='Terminal' || ol.Product2.Family=='Gateway' ){
            a = new Asset();
            a.AccountId = o.AccountId;
            a.Product2Id= ol.PricebookEntry.Product2Id;
            a.Quantity= 1;
            a.Price= ol.UnitPrice;
            a.Name = ol.Product2.Name;
            ol.Converted_to_Asset__c = true;
            update OLI;
            insert ast;

And my Test class is like so where the error is occuring:

public class OpportunityAssetonClosedWonTest 
    static testMethod void closedOpportunity() 
        Account testAcc = new Account(Name = 'TestAccount');
        insert testAcc;

        Pricebook2 p = new Pricebook2(Name = 'Testbook');
        insert p;

        Opportunity testOpportunity = new Opportunity(
            StageName = 'Sourcing Demand',
            CloseDate = Date.newInstance(2017,12,31),
            AccountId = testAcc.Id,
            Name = 'Test Opportunity Triggers',
        insert testOpportunity;

        Pricebook2 pb22 = new Pricebook2(Name='testDIE');
        insert pb22;

        Product2 pro2 = new Product2(Name='BXCD', isActive=true ,Family = 'Terminal');
        insert pro2;

        PricebookEntry pbe2 =new PricebookEntry(UnitPrice=10000,Product2Id=pro2.Id,Pricebook2Id=pb22.id,
                                             isActive=true,UseStandardPrice = false);
        insert pbe2;

        OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id);
        insert OPplineitem2;        


            testOpportunity.StageName = 'Closed/Won';
            update testOpportunity;


You need to insert a standard price. You can do so like this:

insert new PriceBookEntry(Product2Id=pro2.Id, Pricebook2Id=Test.getStandardPricebookId(), UnitPrice=0);

This would be right after inserting the product, but before inserting the other PricebookEntry value.

| improve this answer | |
  • quick question how do i get the id for that pricebook entry seeing as we didn't declare a name for it – Nebbyyy Mar 3 '17 at 17:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.