1

I have an apex class with some code like this:

public List<Product2> myList{
    get{
        myList = [Select p.url__c, p.ProductCode, p.Nom_du_produit_2__c, p.Description From Product2 p WHERE ...];
        return myList; 
    }
    set;
}

I also have a test class, with a system.debug like this for each of my Lists:

system.debug(Object.myList);

This work very well, my apex code is 100% covered.

There is my problem: How can i select just one field in my table Product2 ?

I tried this :

public Product2 MyDescription = [Select Description From Product2 WHERE IsDeleted = false AND IsActive = true AND ProductCode LIKE 'VEVOKK%FFR' AND Name LIKE '10%' ORDER BY ProductCode ASC LIMIT 1];

But it doesn't work, this put me an error in my test class:

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

Can someone help me please ?

5
  • The exception doesn't mean you can't query that, but that there are no records found and returned. – Samuel De Rycke Jan 14 '15 at 9:15
  • Thanks but my query has 1 result ;) – CotralLab Jan 14 '15 at 10:12
  • Maybe it has now .. but at the time of that error, there clearly were no results in your list or soql results, that's literally what the error states and why you got it .. :) – Samuel De Rycke Jan 14 '15 at 10:58
  • Sorry but i always test my queries with the force.com IDE. I'm sure it was ;) – CotralLab Jan 15 '15 at 7:57
  • You do know that in test context you (by default) can''t access (most of) your production data right ? It's best practice to create isolated test data for your tests. – Samuel De Rycke Jan 15 '15 at 8:15
1

Use below code

 public List<Product2> ListProduct = [Select Description From Product2 WHERE IsDeleted = false 
                                     AND IsActive = true AND ProductCode LIKE 'VEVOKK%FFR' AND 
                                     Name LIKE '10%' ORDER BY ProductCode ASC LIMIT 1];

    if(ListProduct!=null && ListProduct.size()>0){
         //use your Field:ListProduct[0].Description here
         System.debug('My Description Field'+ListProduct[0].Description);
    }

Dont forget to accept answer if its helpful. Tia

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.