Find your content:

Search form

You are here

Use single collection for all methods in class? instance vs class variable help


I'm having a hard understanding using public, static, and global keywords with my variables and methods.

Below is a snippet of my code. What I'm trying to do is upon page load, in my constructor create a Set of accountIDs that the user has access to (8-33 this is working). This set will be used to filter queries used in later methods.

What I'm finding is that public pageReference runSearch() has access to 'terrAccSet', but the public static List getsearchAccounts does not have access to it.

If I change it to public static Set terrAccSet, I don't get data in either of the system.debugs - what can I do?

global with sharing class MyClass {

    public static List<FRM_Metrics_gne__c> accountSearchGmap {get; set;}
    public Set<Id> terrAccSet;
    public List<String> terrIdList;

public MyClass() {

    terrAccSet = new Set<Id>();
    terrIdList = new List<String>();
    Set<Id> grpIdSet = new Set<Id>();

    Id uid = '00570000001R95e'; //member of TWO territories
    //UserTerritory Utid =  [SELECT TerritoryId FROM UserTerritory where UserId = :userInfo.getUserId()];

    List<UserTerritory> Utid =  [SELECT TerritoryId FROM UserTerritory where UserId =: uid ];
    for(UserTerritory usrTerr: Utid){

    List<Group> grp = [Select Id from Group where RelatedID IN :terrIdList];
    for (Group eachgroupd : grp ){

    List<AccountShare> accountidList = [SELECT AccountId,UserOrGroupId FROM AccountShare where UserOrGroupId in :grpIdset];
    //all accounst that the user has access according to territory hiearchy
    for(AccountShare eachas:accountidList ){


public PageReference runSearch() {
    //Has Data
    system.debug('**terrAccSet runSearch**   '+terrAccSet);

public static List<Custom_Object__c> getsearchAccounts(String multiSearchString) {
    //terrAccSet variable is missing
    system.debug('**terrAccSet getSearchAccounts**   '+terrAccSet);
        return accountSearchGmap;


Attribution to: tsalb

Possible Suggestion/Solution #1

Why are you choosing for things to be static when you choose for them to be static?

In general static members won't have access to instance members (non-static).

So take the static off of the acctSearchMap property and the getSearchAccounts method, and I would guess you would be in shape.

If the method and variables have to remain static (because of JS remoting, for instance), then consider making terrAccSet static as well. Then you'll need to put the logic for populating the terrAccSet into a static initializer.

static {
//put your init logic here.

But making it static, you're not really gaining anything by putting it in the static initializer, to be honest. It won't be put in the view state, so each remote call will have to reconstruct everything anyway. So you're just as well taking out all the extra stuff and just writing your logic into your getSearchAccounts method.

Attribution to: pchittum
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content