You need to sign in to do that
Don't have an account?
Saurabh Sood 12
How to access database records inside test method
Hi,
I'm writitng test class for this method but unable to get inside if(pr.size() > 0) condition.
Can anyone help me out how to resolved it .
public void fetchPriceDetails(){
try
{
system.debug('----------------->'+prod);
for(Product__c p : prod)
{
pr = [SELECT List_Price__c FROM Pricing__c WHERE Location__c =: p.Location__c AND Sub_Product__c =: p.Sub_Product__c AND Product_Detail__c =: p.Product_Details__c AND Duration_Type__c =: p.Select_Duration__c AND Centre__c =: p.Centre__c];
if(pr.size() > 0){
for(Pricing__c d: pr){
if(d.List_Price__c != null || d.List_Price__c != 0)
p.List_Price_1__c = pr[0].List_Price__c;
}
pList.add(p);
pr.clear();
system.debug('-------->'+pList);
}
}
catch(Exception e)
{
// ApexPages.addMessages(e);
}
}
}
I'm writitng test class for this method but unable to get inside if(pr.size() > 0) condition.
Can anyone help me out how to resolved it .
public void fetchPriceDetails(){
try
{
system.debug('----------------->'+prod);
for(Product__c p : prod)
{
pr = [SELECT List_Price__c FROM Pricing__c WHERE Location__c =: p.Location__c AND Sub_Product__c =: p.Sub_Product__c AND Product_Detail__c =: p.Product_Details__c AND Duration_Type__c =: p.Select_Duration__c AND Centre__c =: p.Centre__c];
if(pr.size() > 0){
for(Pricing__c d: pr){
if(d.List_Price__c != null || d.List_Price__c != 0)
p.List_Price_1__c = pr[0].List_Price__c;
}
pList.add(p);
pr.clear();
system.debug('-------->'+pList);
}
}
catch(Exception e)
{
// ApexPages.addMessages(e);
}
}
}
Greetings to you!
Annotate your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.
Considerations for the @IsTest(SeeAllData=true) Annotation
Always try to avoid use SeeAllData= true because in that case, your test class depends on your sandbox data. You may get some issue will deployment. Instead of using SeeAllData=true, create test data in your test class.
Please refer to the below links which might help you further with the above requirement.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm
https://developer.salesforce.com/forums/?id=906F0000000DBqmIAG
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks and Regards,
Khan Anas
Thanks for reply,
My question is Pricing__c is object where all products prices are stored and in logic i'm querying forn database. Right !!.
and I created records for pricing in test class. so how can I quering records to store value in pr list so it can assign value for product pricing.