0

When I try to save the code (API version 58.0), I keep getting a compile error stating:

Error: Compile Error: Variable is not visible: MSTimeCalculator_CC.priorityMap at line 5:13

Below the apex class:

global class MSTimeCalculator_CC 
implements Support.MilestoneTriggerTimeCalculator {
    global static Map<String,Integer> priorityMap {
        get {
            priorityMap = new Map<String,Integer>();
            priorityMap.put('Urgent',240);
            priorityMap.put('High',480);
            priorityMap.put('Standard',960);
            return priorityMap;
        }
    }
    
     global Integer calculateMilestoneTriggerTime(String caseId, String milestoneTypeId){
        Case c = [SELECT Priority FROM Case WHERE Id=:caseId];
        return priorityMap.get(c.Priority);      
     }
    
}

Any workaround for this?

2 Answers 2

3

When you specify a getter for a property and only a getter, then that property is read only. You can't set a value for it even inside of its getter.

There are a few possible fixes here:

  • Use a "local" variable inside of your getter instead of trying to use the property's name
    • Similar to the above, you can reference/return a different static variable
  • Add a setter (will work, even if it's private)

The approach I'd personally take would be to add a setter, and to check to see if the variable is already set (there's little sense in re-running code if the value has already been set). That, in practice, makes the property "lazy" (it doesn't get a value until the first time it is used).

// global is generally a mistake
// unless you're making a managed package or otherwise know that you need
//   global visibility, it's better to use public
public static Map<String, Integer> priorityMap {
    get {
        // Only do the work if we haven't populated the property yet
        if(priorityMap == null) {
            priorityMap = new Map<String, Integer>();
            priorityMap.put('Urgent', 240);
            priorityMap.put('High', 480);
            priorityMap.put('Standard', 960);
        }

        return priorityMap;
    }
    // Private modifier if you don't want others to be able to set this value
    // If you specify a getter, you MUST specify a setter if you want to
    //   store a value
    private set {}
}
1
  • 1
    As another note, you should be careful when mixing usage of static and instance variables/methods. While instance methods can access static variables (without prefixing with <class name>., which still strikes me as odd), static variables/methods cannot access instance variables/methods. Personally, I always use <class name>.<static variable or method name> when accessing static things from non-static code. Helps to remind me that I'm working with static things and is more consistent style-wise. Also reduces the chance of shadowing.
    – Derek F
    Commented Oct 18, 2023 at 12:48
0

The compile error that you are getting is because the priorityMap property is declared as global static, which means it is only visible within the MSTimeCalculator_CC class. To access it from outside the class, you need to either make it public static or provide a getter method for it. For example, you can change the declaration to:

public static Map<String,Integer> priorityMap {
   get {
        priorityMap = new Map<String,Integer>();
        priorityMap.put('Urgent',240);
        priorityMap.put('High',480);
        priorityMap.put('Standard',960);
        return priorityMap;
     }
}

Or you can add a getter method like:

global static Map<String,Integer> getPriorityMap() {
   return priorityMap;
}

Then use MSTimeCalculator_CC.getPriorityMap() to access it from outside the class.

PS::

I have updated the code and tested it on the org. No compile error was given.

public class MSTimeCalculator_CC implements Support.MilestoneTriggerTimeCalculator {
    public static Map<String,Integer> priorityMap {
        get {
            if(priorityMap == null) {
                priorityMap = new Map<String,Integer>();
                priorityMap.put('Urgent',240);
                priorityMap.put('High',480);
                priorityMap.put('Standard',960);
            }
            return priorityMap;
        }
        
        set {}
    }
    
    public Integer calculateMilestoneTriggerTime(String caseId, String milestoneTypeId){
        Case c = [SELECT Priority FROM Case WHERE Id=:caseId];
        return priorityMap.get(c.Priority);      
    }    
}
3
  • 2
    Global visibility is more permissive than public visibility, and OP is having trouble accessing the variable within the class in which it's defined. Using global is generally a mistake, but it's not the cause of the issue here.
    – Derek F
    Commented Oct 18, 2023 at 11:53
  • Thanks for the comment, @Tushar. I tried to change it to public and still got the same error.
    – Tom
    Commented Oct 19, 2023 at 5:24
  • @Tom I have updated my answer please check. Commented Oct 19, 2023 at 5:38

You must log in to answer this question.

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