+ Start a Discussion
אביב קסוטואביב קסוטו 

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

I am trying to display a selectList in a visualforce page using a custom controller i built.

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

here is my page:
 

<apex:page Controller="BpmIcountPayment">
    <apex:param name="first_name" value="Account.FirstName"/>
    <apex:param name="last_name" value="Account.LastName"/>
    <apex:param name="id" value="Account.idnumber__c"/>
    <apex:param name="address" value="Account.Address"/>
    <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')];

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

        return products;
    }
}
Best Answer chosen by אביב קסוטו
GauravendraGauravendra
Hi,
As you are trying to get the account id from the URL. 
Have you tried passing any id of account to the url when previewing the visualforce.

preview url : 
https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?core.apexpages.request.devconsole=1
after apex/vfpage_name pass the id of any account like this: 
https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?id=0012800000toEnM
Please try this, if the problem sustain then let me know.

All Answers

GauravendraGauravendra
Hi,
As you are trying to get the account id from the URL. 
Have you tried passing any id of account to the url when previewing the visualforce.

preview url : 
https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?core.apexpages.request.devconsole=1
after apex/vfpage_name pass the id of any account like this: 
https://xyz-dev-ed--c.ap6.visual.force.com/apex/vfpage_name?id=0012800000toEnM
Please try this, if the problem sustain then let me know.
This was selected as the best answer
אביב קסוטואביב קסוטו

Tried that, still the same error.

Also just to clarify, currently i am only looking to display the values of the query in the "getProductLov" function.

I have changed my visualforce page to this now:

<apex:page Controller="BpmIcountPayment">
    <apex:form >
        <apex:selectList value="{!productsTitle}" multiselect="false">
            <apex:selectOptions value="{!ProductsLov}"></apex:selectOptions>
        </apex:selectList>
    </apex:form>
</apex:page>
אביב קסוטואביב קסוטו
You were right all along! I was passing an invalid id in the url. after passing a valid id in the url parameters the page displays correctly. Thank you!