You need to sign in to do that
Don't have an account?
Paulo 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: []
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: []
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
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:
You can take the below sample code to insert product, pricebook, oli.
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
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() ?
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/
:)