3

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;

//Constructor
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){
        terrIdList.add(usrTerr.TerritoryId);
    }

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

    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 ){
        terrAccSet.add(eachas.AccountId);
    }

}

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);
        //logic
        return accountSearchGmap;
}

}

1

1 Answer 1

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.

2
  • I'm fairly new to this, but I think getSearchAccounts needs to be static because the other developer is using JS remoting, and is calling it from a global static method. With that in mind - does that frame this a little better? I'm hesitant to post full code because it's pretty messy.
    – tsalb
    Commented Oct 22, 2012 at 23:15
  • The JSRemoting explains the static. I've amended my answer. Hopefully it helps. There might be some other reason for using the territory ID set that I can't see since you are excerpting your code, but not knowing more, I'd move all my logic into the remote method.
    – pchittum
    Commented Oct 22, 2012 at 23:33

You must log in to answer this question.

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