Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Namespace, Class, and Variable Name Precedence
- The parser first assumes that name1 is a local variable with name2 - nameN as field references.
- If the first assumption does not hold true, the parser then assumes that name1 is a class name and name2 is a static variable name with name3 - nameN as field references.
- If the second assumption does not hold true, the parser then assumes that name1 is a namespace name, name2 is a class name, name3 is a static variable name, and name4 - nameN are field references.
- If the third assumption does not hold true, the parser reports an error.
- The parser first assumes that name1 is a local variable with name2 - nameM as field references, and nameN as a method invocation.
- If the first assumption does not hold true:
- If the expression contains only two identifiers (name1.name2()), the parser then assumes that name1 is a class name and name2 is a method invocation.
- If the expression contains more than two identifiers, the parser then assumes that name1 is a class name, name2 is a static variable name with name3 - nameM as field references, and nameN is a method invocation.
- If the second assumption does not hold true, the parser then assumes that name1 is a namespace name, name2 is a class name, name3 is a static variable name, name4 - nameM are field references, and nameN is a method invocation.
- If the third assumption does not hold true, the parser reports an error.
However, with class variables Apex also uses dot notation to reference member variables. Those member variables might refer to other class instances, or they might refer to an sObject which has its own dot notation rules to refer to field names (possibly navigating foreign keys).
Once you enter an sObject field in the expression, the remainder of the expression stays within the sObject domain, that is, sObject fields cannot refer back to Apex expressions.
For instance, if you have the following class:
1public class c {
2 c1 c1 = new c1();
3 class c1 { c2 c2; }
4 class c2 { Account a; }
5}Then the following expressions are all legal:
1c.c1.c2.a.name
2c.c1.c2.a.owner.lastName.toLowerCase()
3c.c1.c2.a.tasks
4c.c1.c2.a.contacts.size()