@@ -14,8 +14,25 @@ public with sharing class Environment {
14
14
return allVariables ;
15
15
}
16
16
17
+ /**
18
+ * @description A map of global variables that can be accessed from any formula.
19
+ * These should always yield the same value regardless of the context in which they are used.
20
+ */
17
21
private static final Map <String , Object > GLOBAL_VARIABLES = new Map <String , Object >();
18
22
23
+ /**
24
+ * @description A map of global context variables that can be accessed from any formula.
25
+ * These are specific to the context in which they are used, and are reset after each evaluation.
26
+ */
27
+ private static final Map <String , Object > GLOBAL_CONTEXT_VARIABLES = new Map <String , Object >();
28
+
29
+ private static Map <String , Object > getAllGlobalVariables () {
30
+ Map <String , Object > allGlobalVariables = new Map <String , Object >();
31
+ allGlobalVariables .putAll (GLOBAL_VARIABLES );
32
+ allGlobalVariables .putAll (GLOBAL_CONTEXT_VARIABLES );
33
+ return allGlobalVariables ;
34
+ }
35
+
19
36
static {
20
37
GLOBAL_VARIABLES.put ('$label', new CustomLabelVariableResolver .CustomLabelResolverWrapper());
21
38
GLOBAL_VARIABLES.put ('$custommetadata', new CustomMetadataVariableResolver .CmtApiName());
@@ -33,11 +50,11 @@ public with sharing class Environment {
33
50
34
51
public static void addGlobalVariable (String name , Object value ) {
35
52
// Check that the name has not already been declared
36
- if (GLOBAL_VARIABLES .containsKey (name .toLowerCase ())) {
53
+ if (GLOBAL_CONTEXT_VARIABLES .containsKey (name .toLowerCase ())) {
37
54
throw new EnvironmentException (' Variable or function with name ' + name + ' already exists' );
38
55
}
39
56
40
- GLOBAL_VARIABLES .put (name .toLowerCase (), value );
57
+ GLOBAL_CONTEXT_VARIABLES .put (name .toLowerCase (), value );
41
58
}
42
59
43
60
public Boolean hasFunction (String functionName ) {
@@ -53,7 +70,7 @@ public with sharing class Environment {
53
70
}
54
71
55
72
// Otherwise, look in the global variables
56
- return (Expr .FunctionDeclaration )GLOBAL_VARIABLES .get (functionName .toLowerCase ());
73
+ return (Expr .FunctionDeclaration )GLOBAL_CONTEXT_VARIABLES .get (functionName .toLowerCase ());
57
74
}
58
75
59
76
/**
@@ -64,7 +81,7 @@ public with sharing class Environment {
64
81
*/
65
82
public static void addGlobalContextVariable (String name , Object value ) {
66
83
String prefixedName = ' @' + name ;
67
- GLOBAL_VARIABLES .put (prefixedName .toLowerCase (), value );
84
+ GLOBAL_CONTEXT_VARIABLES .put (prefixedName .toLowerCase (), value );
68
85
}
69
86
70
87
public Environment () {}
@@ -89,7 +106,7 @@ public with sharing class Environment {
89
106
public Boolean contains (String field ) {
90
107
String lowerCaseField = field .toLowerCase ();
91
108
92
- if (getVariables ().containsKey (lowerCaseField ) || GLOBAL_VARIABLES .containsKey (lowerCaseField )) {
109
+ if (getVariables ().containsKey (lowerCaseField ) || getAllGlobalVariables () .containsKey (lowerCaseField )) {
93
110
return true ;
94
111
}
95
112
@@ -135,8 +152,9 @@ public with sharing class Environment {
135
152
}
136
153
137
154
// Look in global variables
138
- if (GLOBAL_VARIABLES .containsKey (lowerCaseField )) {
139
- return GLOBAL_VARIABLES .get (lowerCaseField );
155
+ Map <String , Object > allGlobalVariables = getAllGlobalVariables ();
156
+ if (allGlobalVariables .containsKey (lowerCaseField )) {
157
+ return allGlobalVariables .get (lowerCaseField );
140
158
}
141
159
142
160
if (getContext () == null ) {
@@ -180,7 +198,7 @@ public with sharing class Environment {
180
198
181
199
public class ClearContext implements EvaluatorEventListener {
182
200
public Map <String , Object > handle (EvaluatorEvent event ) {
183
- GLOBAL_VARIABLES .clear ();
201
+ GLOBAL_CONTEXT_VARIABLES .clear ();
184
202
return null ;
185
203
}
186
204
}
0 commit comments