1

我正在尝试编写我的第一个控制器扩展,但遇到了一个错误:

错误:编译错误:字段表达式的初始项必须是具体的 SObject:第 26 行第 34 列的 LIST

这是产生错误的代码:

public class CustFinAcctRelatedList{
private households__Household__c RG;

private List<households__HouseholdMember__c> RGMembs;
private List<Account> Accts;
private List<Financial_Account__c> finAccts;

//Get Household Record from VisualForce Page//
public CustFinAcctRelatedList(ApexPages.StandardController controller) {
    this.RG = (households__Household__c)controller.getRecord();
}

public List<Financial_Account__c> getFinAccts()
{
    //Get Household Members (junction object between Account and Household) from Household//
    RGMembs = [
        SELECT id, households__Household__c, households__Account__c
        FROM households__HouseholdMember__c 
        WHERE households__HouseholdMember__c.households__Household__c = :RG.id
    ];

    //Get Accounts from Household Members (junction object between Account and Household)//
    Accts = [
        SELECT id
        FROM Account
        //****THE FOLLOWING LINE IS CAUSING THE ERROR****//
        WHERE Account.id IN :RGMembs.households__Account__c.id
    ];

    //Get Financial Account from Accounts//
    finAccts = [
        SELECT id, name, Client__r.name, Current_Value__c, Account_Type__c
        FROM Financial_Account__c
        WHERE Financial_Account__c.Client__c IN :Accts.id
    ];

    return finAccts;
}
}

知道为什么会产生这个错误吗?

我将使用返回数据在 Visualforce 页面上创建自定义相关列表。

谢谢!

4

1 回答 1

1

RGMembs此时是一个列表,因此您需要使用索引器,例如RGMembs[0].households__Account__c.id

您还可以LIMIT 1在查询中使用语法,因为这将分配一个对象作为查询的结果。不过要小心,如果查询返回 0 行,那么它将引发异常。

同样在 RGMembs 查询中,您需要选择households__Acount__c.id否则当您尝试引用 Accts 查询中未查询的字段时会遇到问题。

再看看您的finAccts查询,您最终会遇到与之前查询相同的具体 SObject 错误。要解决这个问题,只需删除.idIN 语句中的IN :Accts

希望这会有所帮助。

- 更新 -

回顾您的代码,我认为您不需要中间查询。第一个查询为您提供了属于家庭成员的所有帐户。然后,您可以使用此帐户列表来帮助您选择与这些帐户对象相关的所有财务帐户。我会这样实现:

Set<Id> RGMembers = new Set<Id>();

for(households__HouseholdMember__c member :  [SELECT households__Account__c.id FROM households__HouseholdMember__c WHERE households__HouseholdMember__c.households__Household__c = :RG.id]){
    RGMembers.add(member.households__Account__c.id);
}

finAccts = [SELECT id, name, Client__r.name, Current_Value__c, Account_Type__c FROM Financial_Account__c WHERE Financial_Account__c.Client__c.id IN :RGMembers];

这更接近您想要实现的目标吗?

于 2012-07-03T16:03:51.983 回答