Search code examples
salesforcevisualforceapexsoql

List has no rows for assignment to SObject error although query returns rows


I'm a bit new to apex and I am trying to display a selectList in a visualforce page using a custom controller i built.

I get a "List has no rows for assignment to SObject" error when trying to preview the visualforce page, but running the query in the developer console, returns the rows.

here is my page:

<apex:page Controller="BpmIcountPayment">
<apex:form >
    <apex:selectList value="{!productsTitle}" multiselect="false">
        <apex:selectOptions value="{!ProductsLov}"></apex:selectOptions>
    </apex:selectList>
</apex:form>
</apex:page>

and my controller:

public class BpmIcountPayment{

    private final Account account;

    public String productsTitle {
      get { return 'products for sale'; }
      set;
    }

    public BpmIcountPayment() {
        account = [SELECT Id, Name, Site FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public List<SelectOption> getProductsLov() {
        List<SelectOption> products = new List<SelectOption>();
        List<Product2> productsList = [SELECT Id, Name, Family 
                                       FROM Product2 
                                       WHERE (Family = 'ShopProduct') 
                                       OR (Family = 'CourseParent') 
                                       OR (Family = 'SFCourseProgram')];

        for (Product2 currProduct : productsList) {
            products.add(new SelectOption(currProduct.Id, currProduct.Name));
        }

        return products;
    }
}

Just to clarify the query i'm referring to is the query in getProductsLov().

My API version is 40 and i am working in a sandbox environment.


Solution

  • Impossible. If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. This eliminates getProductsLov(unless you didn't post whole code) because there you assign to a list.

    Humo(u)r me and System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); in your constructor before firing that query...

    You're viewing the page with valid Account Id passed in the URL? And that Account is visible for your current user? If the page is account-specific, try using <apex:page standardController="Account" extensions="BpmIcountPayment">... (you'll have to provide a different constructor in apex first). This could simplify your code a lot.

    public BpmIcountPayment(ApexPages.StandardController sc){
        if(String.isBlank(sc.getId()){
            System.debug('you screwed up passing the valid acc id');
        } else {
            acc = (Account) sc.getRecord();
        }
    }