【问题标题】:List has no rows for assignment to SObject error although query returns rows尽管查询返回行,但列表没有分配给 SObject 错误的行
【发布时间】:2018-02-06 14:29:10
【问题描述】:

我对 apex 有点陌生,我正在尝试使用我构建的自定义控制器在 visualforce 页面中显示 selectList。

我在尝试预览 visualforce 页面时收到“列表没有分配给 SObject 的行”错误,但在开发人员控制台中运行查询会返回行。

这是我的页面:

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

和我的控制器:

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;
    }
}

只是为了澄清我所指的查询是getProductsLov()中的查询。

我的 API 版本是 40,我在沙盒环境中工作。

【问题讨论】:

    标签: salesforce visualforce apex soql


    【解决方案1】:

    不可能。如果您收到“列表没有可分配给 sObject 的行”,则表示您正在分配给单个对象。这消除了getProductsLov(除非您没有发布整个代码),因为您在那里分配了一个列表。

    Humo(u)r me 和 System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters())); 在您的构造函数中触发该查询...

    您正在查看在 URL 中传递了有效帐户 ID 的页面?您当前的用户可以看到该帐户吗?如果页面是特定于帐户的,请尝试使用&lt;apex:page standardController="Account" extensions="BpmIcountPayment"&gt;...(您必须首先在 apex 中提供不同的构造函数)。这可以大大简化您的代码。

    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();
        }
    }
    

    【讨论】:

    • 你是对的!将有效的“id”参数添加到 url 后,页面正确显示。谢谢!
    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 2023-04-05
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多