You need to sign in to do that
Don't have an account?
devloper sfdc
error display List has no row for assignment to sObject in test class
Hello All,
I have written Apex test class but it showing error like"List has no row for assignment to sObject "
my apex class is-
@isTest
public class ProductEntryTest {
@isTest
public static void test()
{
List<opportunityLineItem> Oli=new List<opportunityLineItem>();
Account acc=new Account();
acc.name='MassBay';
insert acc;
Opportunity opp=new Opportunity();
string p;
opp.CloseDate=date.today().addMonths(2);
opp.Name='test opp';
opp.StageName='IsWon';
opp.AccountId=acc.id;
insert opp;
Product2 prd1 = new Product2 (); // ----> Create product
prd1.Name='Accomodation';
prd1.Units_of_Measure__c='1';
insert prd1;
Pricebook2 pb2 = [select id, name from Pricebook2 where name='BlackBeltHelp V2'];
if(pb2!=null)
{
p=pb2.id;
}
PricebookEntry pbe1 = new PricebookEntry (); //------->Create PriceBookEntry
pbe1.Product2ID=prd1.id;
pbe1.Pricebook2ID=p;
pbe1.UnitPrice=50;
pbe1.isActive=true;
insert pbe1;
OpportunitylineItem otm=new Opportunitylineitem();
otm.OpportunityId=opp.id;
otm.PricebookEntryId=pbe1.id;
otm.Product2Id=prd1.id;
otm.Unit_of_measure__c=prd1.Units_of_Measure__c;
oli.add(otm);
if(oli.size()>1)
{
insert oli;
}
}
}
I have written Apex test class but it showing error like"List has no row for assignment to sObject "
my apex class is-
@isTest
public class ProductEntryTest {
@isTest
public static void test()
{
List<opportunityLineItem> Oli=new List<opportunityLineItem>();
Account acc=new Account();
acc.name='MassBay';
insert acc;
Opportunity opp=new Opportunity();
string p;
opp.CloseDate=date.today().addMonths(2);
opp.Name='test opp';
opp.StageName='IsWon';
opp.AccountId=acc.id;
insert opp;
Product2 prd1 = new Product2 (); // ----> Create product
prd1.Name='Accomodation';
prd1.Units_of_Measure__c='1';
insert prd1;
Pricebook2 pb2 = [select id, name from Pricebook2 where name='BlackBeltHelp V2'];
if(pb2!=null)
{
p=pb2.id;
}
PricebookEntry pbe1 = new PricebookEntry (); //------->Create PriceBookEntry
pbe1.Product2ID=prd1.id;
pbe1.Pricebook2ID=p;
pbe1.UnitPrice=50;
pbe1.isActive=true;
insert pbe1;
OpportunitylineItem otm=new Opportunitylineitem();
otm.OpportunityId=opp.id;
otm.PricebookEntryId=pbe1.id;
otm.Product2Id=prd1.id;
otm.Unit_of_measure__c=prd1.Units_of_Measure__c;
oli.add(otm);
if(oli.size()>1)
{
insert oli;
}
}
}
Use this code:
There reason you are getting this error is because
Pricebook2 pb2 = [select id, name from Pricebook2 where name='BlackBeltHelp V2'];
this query is not returning any results.
You can either choose to create a Pricebook record in your test class. Or simply set seeAllData=true as I did in the code.
Regards,
Nads
Thanks!