Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/public/packages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packageId": "04tRb000004400jIAA",
"packageId": "04tRb0000044fYYIA",
"componentPackageId": "04tRb0000012Mv8IAE"
}
33 changes: 31 additions & 2 deletions expression-src/main/src/interpreter/ContextResolver.cls
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ public with sharing class ContextResolver implements Visitor {
}

public Object visit(Expr.MergeFieldOrScopeVariable mergeField) {
if (this.queryStackDepth > 0) {
// Merge fields are not supported within query expressions, since they
// are used to build the query itself.
return null;
}

String fieldName = mergeField.name.lexeme.toLowerCase();
// Check if it is ignored within a function scope
if (ignoredScopedVariables.contains(fieldName)) {
Expand Down Expand Up @@ -422,9 +428,32 @@ public with sharing class ContextResolver implements Visitor {
return null;
}

private Integer queryStackDepth = 0;
public Object visit(Expr.Query query) {
// Nothing to resolve when inside of a Query expression.
// A query expression essentially starts an isolated context.
this.queryStackDepth++;
if (query.fieldsExpression != null) {
resolve(query.fieldsExpression);
}

if (query.orderBy != null) {
for (Expr orderByExpr : query.orderBy) {
resolve(orderByExpr);
}
}

if (query.limitExpr != null) {
resolve(query.limitExpr);
}

if (query.whereExpr != null) {
resolve(query.whereExpr);
}

if (query.offsetExpr != null) {
resolve(query.offsetExpr);
}

this.queryStackDepth--;
return null;
}

Expand Down
31 changes: 31 additions & 0 deletions expression-src/spec/language/global-context/GlobalContextTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,37 @@ private class GlobalContextTest {
Assert.areEqual(1, Limits.getQueries(), 'Expect one query to retrieve the TargetAccount record');
}

@IsTest
private static void canReferenceCustomRecordContextFieldsInQueries() {
Account accountRecord = new Account(Name = 'Acme');
insert accountRecord;

CustomRecordContext customRecordContext = new CustomRecordContext('TargetAccount', accountRecord.Id);

String expressionFormula = 'QUERY(Account(where: Id = @TargetAccount.Id)[Name])';
Object result = Evaluator.run(expressionFormula, customRecordContext, new Configuration());

List<Object> queryResults = (List<Object>)result;
Assert.areEqual(1, queryResults.size(), 'Expect one record returned from the query');
}

@IsTest
private static void canReferenceCustomRecordContextFieldsInQueries_differentSObjects() {
Account accountRecord = new Account(Name = 'John');
insert accountRecord;

Contact someContact = new Contact(FirstName = 'John', LastName = 'Doe', AccountId = accountRecord.Id);
insert someContact;

CustomRecordContext customRecordContext = new CustomRecordContext('TargetAccount', accountRecord.Id);

String expressionFormula = 'QUERY(Contact(where: FirstName = @TargetAccount.Name)[FirstName])';
Object result = Evaluator.run(expressionFormula, customRecordContext, new Configuration());

List<Object> queryResults = (List<Object>)result;
Assert.areEqual(1, queryResults.size(), 'Expect one record returned from the query');
}

@IsTest
private static void canChangeTheCustomContextPrefix() {
Account accountRecord = new Account(Name = 'Acme');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<apiVersion>65.0</apiVersion>
<status>Active</status>
</ApexClass>
5 changes: 3 additions & 2 deletions sfdx-project_packaging.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"package": "Expression",
"versionName": "Version 1.36",
"versionNumber": "1.46.0.NEXT",
"versionNumber": "1.47.0.NEXT",
"path": "expression-src",
"default": false,
"versionDescription": "Expression core language",
Expand Down Expand Up @@ -75,6 +75,7 @@
"Expression@1.43.0-1": "04tRb000003xzXCIAY",
"Expression@1.44.0-1": "04tRb000003z0hJIAQ",
"Expression@1.45.0-1": "04tRb0000042CNlIAM",
"Expression@1.46.0-1": "04tRb000004400jIAA"
"Expression@1.46.0-1": "04tRb000004400jIAA",
"Expression@1.47.0-1": "04tRb0000044fYYIAY"
}
}