[英]List has no rows for assignment to SObject error although query returns rows
我對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,我正在沙盒環境中工作。
不可能。 如果您獲得“列表沒有要分配給sObject的行”,則意味着您正在分配給單個對象。 這消除了getProductsLov
(除非您沒有發布完整的代碼),因為您在那里分配了一個列表。
我和System.debug(JSON.serializePretty(ApexPages.currentPage().getParameters()));
在觸發該查詢之前在構造函數中...
您正在查看的頁面中帶有在URL中傳遞的有效帳戶ID? 該帳戶對您當前的用戶可見嗎? 如果頁面是特定於帳戶的,請嘗試使用<apex:page standardController="Account" extensions="BpmIcountPayment">...
(您必須首先在頂點中提供其他構造函數)。 這可以大大簡化您的代碼。
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.