@@ -42,7 +42,7 @@ public with sharing abstract class EvaluatorResolver {
4242 return null ;
4343 }
4444
45- ContextResolver contextResolver = new ContextResolver (recordId , new List <Expr .FunctionDeclaration >(), ' @' + DEFAULT_CONTEXT_KEY );
45+ ContextResolver contextResolver = new ContextResolver (recordId , new List <Expr .FunctionDeclaration >(), new Set < String > { ' @' + DEFAULT_CONTEXT_KEY } );
4646
4747 for (String formula : formulas ) {
4848 try {
@@ -191,7 +191,8 @@ public with sharing abstract class EvaluatorResolver {
191191
192192 public override Environment prepareEnvironment (List <Expr > expressions ,
193193 List <Expr .FunctionDeclaration > customFunctionDeclarations , String contextPrefix ) {
194- ContextResolver ctxInterpreter = new ContextResolver (recordId , customFunctionDeclarations , contextPrefix + DEFAULT_CONTEXT_KEY );
194+ ContextResolver ctxInterpreter = new ContextResolver (recordId , customFunctionDeclarations ,
195+ new Set <String > { contextPrefix + DEFAULT_CONTEXT_KEY });
195196 List <SObject > records = ctxInterpreter .build (expressions );
196197 SObject record ;
197198 if (records != null && records .size () > 0 ) {
@@ -214,7 +215,8 @@ public with sharing abstract class EvaluatorResolver {
214215 }
215216
216217 public override Environment prepareEnvironment (List <Expr > expressions , List <Expr .FunctionDeclaration > customFunctionDeclarations , String contextPrefix ) {
217- ContextResolver ctxInterpreter = new ContextResolver (recordIds , customFunctionDeclarations , contextPrefix + DEFAULT_CONTEXT_KEY );
218+ ContextResolver ctxInterpreter = new ContextResolver (recordIds , customFunctionDeclarations ,
219+ new Set <String > { contextPrefix + DEFAULT_CONTEXT_KEY });
218220 List <SObject > records = ctxInterpreter .build (expressions );
219221 // The environment is created without a record, as we are dealing with a list of records,
220222 // so it would not be possible to interpret references to record fields. Instead, the @context
@@ -237,15 +239,35 @@ public with sharing abstract class EvaluatorResolver {
237239
238240 public override Environment prepareEnvironment (List <Expr > expressions , List <Expr .FunctionDeclaration > customFunctionDeclarations , String contextPrefix ) {
239241 Environment env = new Environment ();
242+
243+ Map <SObjectType , List <CustomRecordContext >> contextsBySObjectTypes = new Map <SObjectType , List <CustomRecordContext >>();
240244 for (CustomRecordContext context : this .contexts ) {
241- ContextResolver ctxInterpreter = new ContextResolver (context .recordId , customFunctionDeclarations , contextPrefix + context .key );
242- List <SObject > records = ctxInterpreter .build (expressions );
243- SObject record ;
244- if (records != null && records .size () > 0 ) {
245- record = records [0 ];
245+ SObjectType sObjectType = context .recordId .getSobjectType ();
246+ if (! contextsBySObjectTypes .containsKey (sObjectType )) {
247+ contextsBySObjectTypes .put (sObjectType , new List <CustomRecordContext >());
248+ }
249+ contextsBySObjectTypes .get (sObjectType ).add (context );
250+ }
251+
252+ for (SObjectType currentType : contextsBySObjectTypes .keySet ()) {
253+ List <CustomRecordContext > contextsForType = contextsBySObjectTypes .get (currentType );
254+ Set <Id > idsForType = new Set <Id >();
255+ Set <String > keys = new Set <String >();
256+ Map <Id , CustomRecordContext > contextByRecordId = new Map <Id , CustomRecordContext >();
257+ for (CustomRecordContext context : contextsForType ) {
258+ contextByRecordId .put (context .recordId , context );
259+ idsForType .add (context .recordId );
260+ keys .add (contextPrefix + context .key );
246261 }
247262
248- Environment .addGlobalContextVariable (contextPrefix , context .key , record );
263+ ContextResolver ctxInterpreter = new ContextResolver (idsForType , customFunctionDeclarations , keys );
264+ Map <Id , SObject > recordById = new Map <Id , SObject >(ctxInterpreter .build (expressions ));
265+ for (Id recordId : recordById .keySet ()) {
266+ SObject record = recordById .get (recordId );
267+ CustomRecordContext context = contextByRecordId .get (recordId );
268+
269+ Environment .addGlobalContextVariable (contextPrefix , context .key , record );
270+ }
249271 }
250272
251273 return env ;
0 commit comments