You need to sign in to do that
Don't have an account?
Gopal Chat
In my trigger testing the test is not going inside if trigger.new
trigger Setpricebookentry1 on Product2 (after insert)
{
Set<ID> prodIdSet = Trigger.newMap.keySet();
list<Pricebookentry> listpbe=new list<Pricebookentry>();
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
system.debug('++++++>'+prbook);
for(Product2 po:Trigger.New)
{
system.debug('=====>'+trigger.new);
Pricebookentry pbe=new Pricebookentry();
pbe.UnitPrice=10;
pbe.Pricebook2Id=prbook.id;
pbe.Product2Id=po.id;
listpbe.add(pbe);
}
if(listpbe.size()>0)
{
insert listpbe;
system.debug('------>'+listpbe);
}
}
this is my test code
@istest
public class TestSetpricebookentry1
{
@istest
public static void mytest()
{
Product2 po=new Product2();
po.Name='Raghav';
po.IsActive=true;
insert po;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = pricebookId;
standardPrice.Product2Id =po.Id;
standardPrice.UnitPrice = 10000;
standardPrice.IsActive = true;
insert standardPrice;
}
}
{
Set<ID> prodIdSet = Trigger.newMap.keySet();
list<Pricebookentry> listpbe=new list<Pricebookentry>();
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
system.debug('++++++>'+prbook);
for(Product2 po:Trigger.New)
{
system.debug('=====>'+trigger.new);
Pricebookentry pbe=new Pricebookentry();
pbe.UnitPrice=10;
pbe.Pricebook2Id=prbook.id;
pbe.Product2Id=po.id;
listpbe.add(pbe);
}
if(listpbe.size()>0)
{
insert listpbe;
system.debug('------>'+listpbe);
}
}
this is my test code
@istest
public class TestSetpricebookentry1
{
@istest
public static void mytest()
{
Product2 po=new Product2();
po.Name='Raghav';
po.IsActive=true;
insert po;
Id pricebookId = Test.getStandardPricebookId();
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = pricebookId;
standardPrice.Product2Id =po.Id;
standardPrice.UnitPrice = 10000;
standardPrice.IsActive = true;
insert standardPrice;
}
}
Issue is coming because of below code in your Trigger.
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
You need to create Pricebook2 record in your test class. Dnt use SeeAllData = true in your test class.
Update your code like below
Let us know if this will help you
All Answers
Issue is coming because of below code in your Trigger.
Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
You need to create Pricebook2 record in your test class. Dnt use SeeAllData = true in your test class.
Update your code like below
Let us know if this will help you