4

Does anyone know why I can't do this?

Here's a domain class

public class Opportunities extends fflib_SObjectDomain {
    public static Set<String> DIRECT_RECORD_TYPES = new Set<String> { 'a', 'b', 'c' };
    public static Set<String> INDIRECT_RECORD_TYPES = new Set<String> { 'd', 'e', 'f' };
}

The problem is in this class

public class MyClass {
    public void myMethod() {
        strRecordTypeName = ...

        if (Opportunities.DIRECT_RECORD_TYPES.contains(strRecordTypeName)) {
            // do stuff
        }
    }
}

What I get is:

Compile Error: Variable does not exist: DIRECT_RECORD_TYPES at line x

I'm mystified. I guess I can make a static method that returns the set. I thought maybe it's because Opportunities doesn't have a zero-argument constructor. But I added one and that didn't help. I also tried using a static method instead of a static Set variable to no avail.

2
  • Seems likely you named a variable somewhere in that scope opportunities.
    – Adrian Larson
    Commented Nov 9, 2019 at 0:36
  • +1 for fflib usage
    – cropredy
    Commented Nov 9, 2019 at 6:09

2 Answers 2

3

The only way I can think of to reproduce your error is to introduce a naming conflict. Here is a simple analogue:

public class Foo
{
    public String property = 'abcde';
}
public class Bar
{
    public static void doStuff()
    {
        Object foo = 42;
        system.debug(Foo.property);
    }
}

It seems like within your method you must have named some variable opportunities, making it impossible to reference your domain class.

1
  • 1
    I should slap myself in the head. Higher up in the method I do have a local variable named opportunities. Thanks! It actually happened to me in two different classes so I didn't think to blame myself.
    – Todd
    Commented Nov 9, 2019 at 2:52
0

You need to make your method static. I tried something like this and it works just fine.

public class OpportunitiesHelper {

public static void myMethod() {
    String a = 'a';

    if (Opportunities.DIRECT_RECORD_TYPES.contains(a)) {
    System.debug('It contains');
}

Opportunities is exactly as yours except that I don't extand the fflib_SObjectDomain that I don't have.

Execute this on a Developer Console:

OpportunitiesHelper.myMethod();

OUTPUT:

02:37:58.51 (65634472)|USER_DEBUG|[11]|DEBUG|It contains
3
  • 1
    It is perfectly valid to reference static variables from an instance method.
    – Adrian Larson
    Commented Nov 9, 2019 at 1:42
  • Removing static from the method fires this error: Compile failure on line 12, column 21: Non static method cannot be referenced from a static context: void OpportunitiesHelper.myMethod(). I'm not getting the same error than him. Weird.. Commented Nov 9, 2019 at 1:50
  • That just means that myMethod is referenced somewhere as static. as soon all references are removed - method could be saved as non-static
    – kurunve
    Commented Nov 9, 2019 at 20:39

You must log in to answer this question.

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