-1

I am working on the Account VF page that has a custom controller (AccountPageController). On this page I would like to add a field (customField__c) from a different CustomObject__C.

What I have tried so far is on the AccountPageController I added the following code:

            public with sharing class AccountPageController {
            
                public List<Address__C> contactList {get;set;}
                    public void GetAddressInfo(){
                    contactList=[select CustomField1__c, CustomField2__c, ID1__c, ID2__c from CustomObject__c where ID1__c = :ID or ID2__c = :ID];
    
    }
}

And then on the VF page I have:

 <apex:pageblock >
    <apex:pageBlockTable value="{!contactList}" var="con">
         <apex:facet name="header">Status</apex:facet>
         <apex:outputtext value="{!con.CustomField1__C}"/>
     </apex:pageBlockTable>
  </apex:pageblock>

However, I don't receive an error but nothing shows up in that block, even if I add text. I am sure there is something wrong with my method. I'm really starting from scratch here so any help is appreciated.


Ok, so I have added that to my controller for account:

    //start
public List<Address__C> contactList {
   get {
        if (contactList == null) {
           contactList = [select Account1__c, Account2__c, CustomField__c
                           from CustomObject__c  
                            where ACCOUNT1__c = :acct.id or ACCOUNT2__c = :acct.id];
        }
        return contactList;
    } private set;
}

//end

However I don't get any errors, but I don't see those fields from the customobject__c on the account page. Where on the controller page should that code go? Does it need to go into a certain area, does it need to be a constructor? Thanks.

1 Answer 1

1

You need to modify your getter so it actually fetches the values from the object when requested. Here, I'm using a lazy load pattern.

public List<Address__C> contactList {
   get {
        if (contactList == null) {
           contactList = [select CustomField1__c, CustomField2__c, ID1__c, ID2__c 
                            from CustomObject__c 
                            where ID1__c = :ID or ID2__c = :ID];
        }
        return contactList;
    } private set;
}

If the values of this.ID change; then be sure to null out the contactList so a new set of records is fetched when the VF page requests them

1
  • Thanks, so this is what I have in my controller for the account page:
    – gh0st
    Commented Jul 28, 2021 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .