0

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:

ERROR:

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.PurchaseDate=o.CloseDate;
            a.status='Purchased';
            a.Name = ol.Product2.Name;
            ast.add(a);
            ol.Converted_to_Asset__c = true;
        }
        }
            update OLI;
            insert ast;
        }
    }
}

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

@isTest
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',
            Pricebook2Id=p.id
        );
        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;        



        Test.startTest();

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

        Test.stopTest(); 
    }
}

marked as duplicate by sfdcfox apex Mar 3 '17 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

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.

  • 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.