简体   繁体   English

列表没有行分配给SObject错误,尽管查询返回了行

[英]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. 我对apex有点陌生,我正在尝试使用自己构建的自定义控制器在visualforce页面中显示一个selectList。

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. 尝试预览visualforce页面时,出现“列表没有要分配给SObject的行”错误,但是在开发人员控制台中运行查询时,返回这些行。

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() . 只是为了弄清楚我要引用的查询是getProductsLov()的查询。

My API version is 40 and i am working in a sandbox environment. 我的API版本是40,我正在沙盒环境中工作。

Impossible. 不可能。 If you're getting "list has no rows to assign to sObject" it means you're assigning to single object. 如果您获得“列表没有要分配给sObject的行”,则意味着您正在分配给单个对象。 This eliminates getProductsLov (unless you didn't post whole code) because there you assign to a list. 这消除了getProductsLov (除非您没有发布完整的代码),因为您在那里分配了一个列表。

Humo(u)r me and System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); 我和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? 您正在查看的页面中带有在URL中传递的有效帐户ID? 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). 如果页面是特定于帐户的,请尝试使用<apex:page standardController="Account" extensions="BpmIcountPayment">... (您必须首先在顶点中提供其他构造函数)。 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM