function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sw6sw6 

string list in custom controller not populating.

Hi Guys,

 

I have an apex code for a visualforce page but I'm having a bit of an issue returning values from the account object which is one of the master objects in my junction object.

The code below shows the part of my code where i have identified the issue to be from. 

 
public id uuid = UserInfo.getUserid();
    public id acctid = [select accountid from user where id = :uuid][0].accountid;
    public list<AccountAssociatedSite__c> relatesites = [Select RelatedSite__c,RelatedSite__r.Related_Site__c,RelatedAccount__c,RelatedSite__r.Name From AccountAssociatedSite__c a where RelatedAccount__c = :acctid];    
    public list<String> siteac = new List<String>();    
    public list<AccountAssociatedSite__c>listOfSiteAccounts = new list<AccountAssociatedSite__c>();


public void acssites(){
        for(AccountAssociatedSite__c res : relatesites){
            
            siteac.add(res.RelatedSite__r.Related_Site__c);
            
        }
        
    }
    
    public void acsites(){
        for(string sitename : siteac){
            
                listOfSiteAccounts = [select RelatedAccount__r.Name,RelatedSite__r.Related_Site__c from AccountAssociatedSite__c where RelatedSite__r.Related_Site__c like :sitename];            
            
        }
        
    }
    
     public ApexPages.StandardSetController setAllAccountsOfSites {
        get {
            if(setAllAccountsOfSites == null) {
                setAllAccountsOfSites = new ApexPages.StandardSetController(listOfSiteAccounts);
                }
            return setAllAccountsOfSites;
        }
        set;
     }     
    
    //initialize setAllAccountsOfSites and return records
    public List<AccountAssociatedSite__c> getAccountsOfSites() {
        return (List<AccountAssociatedSite__c>) setAllAccountsOfSites.getRecords();
    }
    

 

The code does not return any errors but I am unable to get any values added to the siteac list of type string - I found this out using the developer console to debug. I'm not sure why. (Related_Site__c is an URL but for some reason apex returns it as a String, and hence my use of  list of type String).

Also AccountAssociatedSite__c is a junction object with the account object as one of its masters. I just want to return a list of accounts which share a site - so for each site, I return all the accounts that use that site.

 

Thanks for any suggestions.

bob_buzzardbob_buzzard

Where do the acssites/acsites get called from?  I can see that acssites iterates the relatedsites list, so presumably its that function that sets up siteac, but I can't find where you are invoking it.

sw6sw6

Hi Bob, thanks for responding. Below is my entire code as I have it. And you were right acssites iterates the relatesites list and populates the siteac list. My intentions were to then iterate over the siteac and then use each iteration as a variable for my soql query which populates the listOfSiteAccounts. I'm not sure what else I need to add or what I need to call additionally to get the acssites and acsite methods pulling in the records. I think this is where I really need help. 

public with sharing class projectsitescontroller {

    public id uuid = UserInfo.getUserid();
    public id acctid = [select accountid from user where id = :uuid][0].accountid;
    public list<AccountAssociatedSite__c> relatesites = [Select RelatedSite__c,RelatedSite__r.Related_Site__c,RelatedAccount__c,RelatedSite__r.Name From AccountAssociatedSite__c a where RelatedAccount__c = :acctid];    
    public list<String> siteac = new List<String>();    
    public list<AccountAssociatedSite__c>listOfSiteAccounts = new list<AccountAssociatedSite__c>();
    
    
    public ApexPages.StandardSetController setAccountSites {
        get {
            if(setAccountSites == null) {
                setAccountSites = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [Select a.RelatedSite__r.Account_Account_Site__c, a.RelatedSite__r.Related_Site__c, a.RelatedSite__r.Name,
                     a.RelatedSite__c, a.RelatedAccount__r.Name, 
                    a.RelatedAccount__c, a.Name From AccountAssociatedSite__c a where RelatedAccount__c = :acctid]));
            }
            return setAccountSites;
        }
        set;
    }    
    
    //initialize setAccounts and return records
    public List<AccountAssociatedSite__c> getMySites() {
        return (List<AccountAssociatedSite__c>) setAccountSites.getRecords();
    }
        
    public void acssites(){
        for(AccountAssociatedSite__c res : relatesites){
            
            siteac.add(res.RelatedSite__r.Related_Site__c);
            
        }
             
    }
    
    
    public void acsites(){
        for(string sitename : siteac){
            
                listOfSiteAccounts = [select RelatedAccount__r.Name,RelatedSite__r.Related_Site__c from AccountAssociatedSite__c where RelatedSite__r.Related_Site__c = :sitename];            
            
        }
                
    }
    
     public ApexPages.StandardSetController setAllAccountsOfSites {
        get {
            if(setAllAccountsOfSites == null) {
                setAllAccountsOfSites = new ApexPages.StandardSetController(listOfSiteAccounts);
                }
            return setAllAccountsOfSites;
        }
        set;
     }     
    
    //initialize setAllAccountsOfSites and return records
    public List<AccountAssociatedSite__c> getAccountsOfSites() {
        return (List<AccountAssociatedSite__c>) setAllAccountsOfSites.getRecords();
    }

    
}

 

bob_buzzardbob_buzzard

The problem is that you don't call either of the acsites/acssites methods, so the siteac property is never populated.

 

As you are setting the properties that the acsites method relies on when constructing the class, I'd imagine you need to be calling one or both of those from the constructor - this is a bit of a guess though!

sw6sw6

Hi Bob, I will add a controller and call the methods within it and reply with a feedback. thanks.

sw6sw6

Hi Bob,  putting the variables and calling the methods inside of  the constructors did partially work. I say this because this method:

public void acsites(){
        for(string sitename : siteac){
            
                listOfSiteAccounts = [select RelatedAccount__r.Name,RelatedSite__r.Related_Site__c from AccountAssociatedSite__c where RelatedSite__r.Related_Site__c = :sitename];            
            System.debug(listOfSiteAccounts);
        }
                
    }

 is supposed to get the list of all the accounts that use a particular site  but it's only showing me just one site(eg. wiki.developerforce.com could be shared by M&M and B&B but I'm only getting M&M and wiki.developerforce.com). Also there are more than one sites and hence siteac should be populated by more than one sites. I'm not sure what I'm currently missing but I'm currently trying to debug in the console.

 

Below is the changes I have made so far based on your suggestion:

public id uuid;
    public id acctid;
    public list<AccountAssociatedSite__c> relatesites;
    public list<String> siteac;
    public list<AccountAssociatedSite__c>listOfSiteAccounts; 

    public projectsitescontroller()
    {
    this.uuid = UserInfo.getUserid();
    this.acctid = [select accountid from user where id = :uuid][0].accountid;
    this.relatesites = [Select RelatedSite__c,RelatedSite__r.Related_Site__c,RelatedAccount__c,RelatedSite__r.Name From AccountAssociatedSite__c a where RelatedAccount__c = :acctid];    
    this.siteac = new List<String>();    
    this.listOfSiteAccounts = new list<AccountAssociatedSite__c>();
    
    acssites();
    acsites();
    
    }