Salesforce (SFDC) – public, static, global keywords – use one list for entire class

apex-codepublicsalesforcestatic methodsvisualforce

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

}

Best Answer

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.

This set should be an instance property, not static. Use static when you want to create a method that does not affect the state of a controller or class, eg. a text parser-text in text out.

You should make the class Global if you want to create a package and make your class available outside your package so that other Apex code can invoke it, or if your class will create webService or REST methods to be exposed.

Public should be used to expose properties to the VisualForce pages that will consume the properties. Otherwise, use Private methods and properties for controller side only processing.

public static List getsearchAccounts(String multiSearchString) { //terrAccSet variable is missing system.debug('terrAccSet getSearchAccounts '+terrAccSet); //logic return accountSearchGmap; }

This method should not be static because it accesses an instance property (it reads state).

Simple rule of thumb, if it is a visualforce page + controller, you shouldn't need anything static to do your normal work of querying the database and returning data to the page.

Related Topic