5

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 with sharing class ACScontroller {

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

public ACScontroller()
{
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();

System.debug('Printing...');

System.debug(siteac);
System.debug(listOfSiteAccounts);

}



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> getBFSites() {
    return (List<AccountAssociatedSite__c>) setAccountSites.getRecords();
}

public void acssites(){
    for(AccountAssociatedSite__c res : relatesites){

        siteac.add(res.RelatedSite__r.Related_Site__c);
        System.debug(siteac);
    }

}


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

}

 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 a 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.

1
  • Where is the List<String> in your code? I think you've stripped too much out of it in your efforts to make it presentable. At this point, it's not valid apex nor accurate pseudocode. Can you make corrections to the markup so that it accurately represents your Lists and methods?
    – Mark Pond
    Commented Sep 19, 2013 at 21:25

1 Answer 1

3

After tidying up your code a little, also not sure how some of the syntax worked either, suspect some was lost in the production of the sample code. What I think your saying you have is this...

public with sharing class ACScontroller
{
    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 String[] siteac = new List<String>();
    public AccountAssociatedSite_c[] listOfSiteAccounts = new List<AccountAssociatedSite__c>();

    public void acssites()
    {
        for(AccountAssociatedSite_c res : relatesites){
            siteac.add(res.RelatedSite_r.Related_Site__c);
        }
    }
}

What your leveraging here is the initialisation of your class member variables during class construction, as can be seen here. While the compiler lets your perform this, its really only ideal for simple data types and not indirectly performing SOQL queries (with other class member dependencies). I have doubts over the order in which the Apex runtime will attempt to initialise your variables (and hence execute the queries) and also how any exceptions will be thrown. Which maybe leading to your issue...

A deterministic way of performing this initialisation logic is to place it in a class constructor, so it would look like the following in your case. Give this a try and see if it helps, certainly you will then be able to insert some System.debug statements between the lines to debug further.

public with sharing class ACScontroller
{
    public id uuid;
    public id acctid;
    public List<AccountAssociatedSite_c> relatesites;
    public String[] siteac;
    public AccountAssociatedSite_c[] listOfSiteAccounts;

    public ACScontroller()
    {
        uuid = UserInfo.getUserid();
        System.debug(uuid);
        acctid = [select accountid from user where id = :uuid][0].accountid;
        System.debug(acctid);
        relatesites = [Select RelatedSite_c,RelatedSite_r.Related_Site_c,RelatedAccount_c,RelatedSite_r.Name From AccountAssociatedSite_c a where RelatedAccount_c = :acctid];
        System.debug(relatesites.size());
        siteac = new List<String>();
        listOfSiteAccounts = new List<AccountAssociatedSite__c>();
    }

    public void acssites()
    {
        for(AccountAssociatedSite_c res : relatesites){
            siteac.add(res.RelatedSite_r.Related_Site__c);
        }
    }
}
2
  • thanks for the pointers and yes I only presented some portion of my code (the area I believe my issue to be coming from). I will give your solution a try and give additional feedback ASAP. thanks. Commented Sep 20, 2013 at 1:50
  • thanks for your suggestion. I made the changes and added a constructor to my code and called my methods inside of it. Debugging from the console reveals that both siteac and listOfSiteAccounts are now being populated. But that just opened another interesting issue which I think might be more appropriate for me to open another topic but here is a gist of it. I am rendering the returned list of accounts and sites in an <apex:pageblocktable> in my visualforce table but it only displays the accounts related to the last site in the siteac list. Commented Sep 20, 2013 at 18:31

You must log in to answer this question.

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