@@ -206,12 +206,12 @@ public with sharing abstract class EvaluatorResolver {
206206 if (records != null && records .size () > 0 ) {
207207 record = records [0 ];
208208 }
209- Environment env = new Environment ( record );
210-
209+ // Register the global context variables before constructing the Environment so that
210+ // the Environment's context snapshot (used as part of its cache-key identity) is complete.
211211 Environment .addGlobalContextVariable (contextPrefix , ' id' , recordId );
212212 Environment .addGlobalContextVariable (contextPrefix , ' context' , record );
213213
214- return env ;
214+ return new Environment ( record ) ;
215215 }
216216 }
217217
@@ -230,15 +230,15 @@ public with sharing abstract class EvaluatorResolver {
230230 }
231231 ContextResolver ctxInterpreter = new ContextResolver (recordContexts , contextPrefix , customFunctionDeclarations );
232232 List <SObject > records = ctxInterpreter .build (expressions );
233- // The environment is created without a record, as we are dealing with a list of records,
234- // so it would not be possible to interpret references to record fields. Instead, the @context
235- // variable should be used to access the list of records.
236- Environment env = new Environment ();
237-
233+ // Register the global context variables before constructing the Environment so that
234+ // the Environment's context snapshot (used as part of its cache-key identity) is complete.
238235 Environment .addGlobalContextVariable (contextPrefix , ' ids' , recordIds );
239236 Environment .addGlobalContextVariable (contextPrefix , ' context' , records );
240237
241- return env ;
238+ // The environment is created without a record, as we are dealing with a list of records,
239+ // so it would not be possible to interpret references to record fields. Instead, the @context
240+ // variable should be used to access the list of records.
241+ return new Environment ();
242242 }
243243 }
244244
@@ -251,8 +251,6 @@ public with sharing abstract class EvaluatorResolver {
251251
252252 public override Environment prepareEnvironment (List <Expr > expressions , List <Expr .FunctionDeclaration > customFunctionDeclarations ,
253253 String contextPrefix ) {
254- Environment env = new Environment ();
255-
256254 Map <SObjectType , List <CustomRecordContext >> contextsBySObjectTypes = new Map <SObjectType , List <CustomRecordContext >>();
257255 for (CustomRecordContext context : this .contexts ) {
258256 SObjectType sObjectType = context .recordId .getSobjectType ();
@@ -288,7 +286,9 @@ public with sharing abstract class EvaluatorResolver {
288286 }
289287 }
290288
291- return env ;
289+ // Construct the Environment after all global context variables have been registered so
290+ // that its context snapshot (used as part of its cache-key identity) is complete.
291+ return new Environment ();
292292 }
293293 }
294294
0 commit comments