+ Start a Discussion
Paulo PerezPaulo Perez 

Test Standarprice

Hello Experts SalesForce,

Below is the test code to standardprice to product2:

@isTest
public class TestaStandardPrice {
static testMethod void testInsertLine() { 
Test.StartTest();
Product2 p = new product2(name='x',isactive=TRUE);
insert p;
System.assertEquals(0.00, [select UnitPrice from PricebookEntry 
where product2id = :p.id].UnitPrice);
Test.StopTest();
}
}

But, show me the fail:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoPopulatePricebookEntry: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.AutoPopulatePricebookEntry: line 2, column 1: []

 
Best Answer chosen by Paulo Perez
Paulo PerezPaulo Perez
Look friends!

I solved changed the test code to:
@isTest (seeAllData=true)

I think so it is related with the apex version:

https://sfdcdev.wordpress.com/2012/02/18/standard-price-book-not-found-in-apex-test-methods/

:)
 

All Answers

James LoghryJames Loghry
By default, at least in recent-ish API versions of Apex, data is not available in your unit test without explicitly using the SeeAllData=true annotation.  This also includes the Standard Price Book.  However, in Summer '14, Salesforce released a method for retrieving the StandardPriceBook without having to use see all data.  The method Test.getStandardPricebookId() returns the standard pricebook id in the test context.

In short, you'll need to modify your trigger to use the getStandardPriceBookId method to pull in the price book Id if the trigger is running in the Test context.  

You can do this by:
  1. Modifying the trigger to check for Test.isRunningTest().  If the test is running, use the getStandardPriceBookId method, otherwise query for it
  2. Or move your logic into an Apex class / Trigger handler class, where you can set the price book id as a variable.

 
Arunkumar RArunkumar R
Hi Paulo Perez,

You can take the below sample code to insert product, pricebook, oli.
@isTest
private class TestOpportunityLineItem
{
static testMethod void insertLineItem()
{
Product2 pr = new Product2();
pr.Name='Moto - G1';
pr.isActive=true;
insert pr;

// Insert Pricebook
PriceBook2 customPriceBook = new PriceBook2();
customPriceBook.Name='Custom Pricebook';
customPriceBook.IsActive=true;
insert customPriceBook;

// Query Standard and Custom Price Books
Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id];
Id stdPriceBookRecId = Test.getStandardPricebookId();

// Create Standard PriceBookEntry
PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
stdPriceBookEntry.Product2Id=pr.Id;
stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId;
stdPriceBookEntry.UnitPrice=2000;
stdPriceBookEntry.IsActive=true;
insert stdPriceBookEntry;

// Create Custom PriceBookEntry
PriceBookEntry customPriceBookEntry = new PriceBookEntry();
customPriceBookEntry.Product2Id=pr.Id;
customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id;
customPriceBookEntry.UnitPrice=5000;
customPriceBookEntry.IsActive=true;
insert customPriceBookEntry;

// Create Opportunity
Opportunity opp = new Opportunity();
opp.Name = 'Test';
opp.CloseDate= System.Today();
opp.StageName='Prospecting';
insert opp;

// Add product and Pricebook to the particular opportunity using OpportunityLineItem 
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = opp.Id;
oppLineItem.PricebookEntryId = customPriceBookEntry.Id;
oppLineItem.UnitPrice = 7000;
oppLineItem.Quantity = 5;
insert oppLineItem;
}
}

 
Paulo PerezPaulo Perez
Hello 
Arunkumar R
I'm Still same fail:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoPopulatePricebookEntry: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.AutoPopulatePricebookEntry: line 2, column 1: []

Paulo
Paulo PerezPaulo Perez
James Loghry

This is my trigger:

trigger AutoPopulatePricebookEntry on Product2 (after insert) {
     sObject s = [select ID from Pricebook2 where IsStandard = TRUE];
     List<PriceBookEntry> toInsert = new List<PriceBookEntry>();
     for (Product2 newProduct: Trigger.new) {
        PricebookEntry z = new PricebookEntry(Pricebook2Id=s.ID,Product2Id=newProduct.ID,UnitPrice=0.00,
            IsActive=TRUE, UseStandardPrice=FALSE);
         toInsert.add(z);
     }
     insert toInsert;

How can i input Test.getStandardPricebookId() ?


 
Paulo PerezPaulo Perez
Look friends!

I solved changed the test code to:
@isTest (seeAllData=true)

I think so it is related with the apex version:

https://sfdcdev.wordpress.com/2012/02/18/standard-price-book-not-found-in-apex-test-methods/

:)
 
This was selected as the best answer