1
votes

I am trying to write my first controller extension, and I am running into an error:

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST at line 26 column 34

This is the code that generated the error:

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

Any idea why this error is being generated?

I am going to use the return data to create a custom related list on a Visualforce page.

Thanks!

1

1 Answers

1
votes

RGMembs is a list at this point so you need to use an indexer such as RGMembs[0].households__Account__c.id

You could also use the LIMIT 1 syntax in your query as this will assign an object as the result of the query. Be careful though with this, if the query returns 0 rows then it will throw an exception.

Also in the RGMembs query you need to select the households__Acount__c.id otherwise you will run into a problem when you try to reference a field that wasn't queried in the Accts query.

Looking also at your finAccts query you will end up with the same concrete SObject error as you had in the earlier query. To fix this one simply remove the .id in your IN statement so it becomes IN :Accts

Hopefully that will help.

-- UPDATE --

Looking back at your code I don't think you need the middle query. The first query gives you all of the accounts that belong to the members in the household. You can then use this list of accounts to help you select all of the Financial Accounts related to those Account objects. I would go about achieving that like this:

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

Is this closer to what you're trying to achieve?