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
1 change: 1 addition & 0 deletions .idea/formula-evaluator.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "04tRb0000044fYYIA",
"packageId": "04tRb0000045WPxIAM",
"componentPackageId": "04tRb0000012Mv8IAE"
}
4 changes: 2 additions & 2 deletions expression-src/main/src/interpreter/ContextResolver.cls
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public with sharing class ContextResolver implements Visitor {
// References to @Context fields will always go to the top level query
// as they are part of the global contextual context tied to the record Id
// from which the Evaluation was started.
addFieldToQuery(this.topLevelQuery, referenceName.toLowerCase());
return addFieldToQuery(this.topLevelQuery, referenceName.toLowerCase());
} else {
this.queryContext.queryBuilder.selectField(referenceName);
}
Expand Down Expand Up @@ -320,7 +320,7 @@ public with sharing class ContextResolver implements Visitor {
// If context is being accessed, then we always want to run the query.
this.shouldExecuteQuery = true;

// recordId migh be null when this is being run from within a subquery.
// recordId might be null when this is being run from within a subquery.
// If so, return early.
if (this.queryContext.recordId == null) {
return null;
Expand Down
7 changes: 6 additions & 1 deletion expression-src/main/src/resolver/EvaluatorResolver.cls
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ public with sharing abstract class EvaluatorResolver {
ContextResolver ctxInterpreter = new ContextResolver(contextsForType, contextPrefix, customFunctionDeclarations);
List<SObject> queriedRecords = ctxInterpreter.build(expressions);
if (queriedRecords == null || queriedRecords.isEmpty()) {
continue;
queriedRecords = new List<SObject>();
for (Id recordId : contextByRecordId.keySet()) {
SObject emptyRecord = currentType.newSObject();
emptyRecord.Id = recordId;
queriedRecords.add(emptyRecord);
}
}
Map<Id, SObject> recordById = new Map<Id, SObject>(queriedRecords);
for (Id recordId : recordById.keySet()) {
Expand Down
19 changes: 11 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"homepage": "https://github.com/cesarParra/formula-evaluator#readme",
"devDependencies": {
"@cparra/apex-reflection": "^2.4.1",
"@cparra/apexdocs": "3.16.1",
"@cparra/apexdocs": "^3.17.0",
"@tailwindcss/forms": "^0.5.6",
"@types/node": "^20.10.2",
"js-yaml": "^4.1.0",
Expand Down
4 changes: 4 additions & 0 deletions sfdx-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"path": "src-pull",
"default": true
},
{
"path": "unpackaged/integration-tests",
"default": false
}
],
"name": "Expression",
Expand Down
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.47.0.NEXT",
"versionNumber": "1.48.0.NEXT",
"path": "expression-src",
"default": false,
"versionDescription": "Expression core language",
Expand Down Expand Up @@ -76,6 +76,7 @@
"Expression@1.44.0-1": "04tRb000003z0hJIAQ",
"Expression@1.45.0-1": "04tRb0000042CNlIAM",
"Expression@1.46.0-1": "04tRb000004400jIAA",
"Expression@1.47.0-1": "04tRb0000044fYYIAY"
"Expression@1.47.0-1": "04tRb0000044fYYIAY",
"Expression@1.48.0-1": "04tRb0000045WPxIAM"
}
}
52 changes: 52 additions & 0 deletions unpackaged/integration-tests/classes/IntegrationTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@IsTest
private class IntegrationTest {
@IsTest
static void usingChildRelationships() {
Account anyAccount = new Account(Name = 'Sample Account');
insert anyAccount;
Contact firstContact = new Contact(FirstName = 'First', LastName = 'Contact', AccountId = anyAccount.Id);
Contact secondContact = new Contact(FirstName = 'Second', LastName = 'Contact', AccountId = anyAccount.Id);
insert new List<Contact> { firstContact, secondContact };

String expressionPiped = '@Context.Contacts -> WHERE(FirstName = "First") -> SIZE() = 1';
Boolean pipedResult = (Boolean)Evaluator.run(expressionPiped, anyAccount.Id);
Assert.isTrue(pipedResult, 'There should be exactly one contact with the first name "First".');

String expressionNested = 'SIZE(WHERE(@Context.Contacts, FirstName = "First")) = 1';
Boolean nestedResult = (Boolean)Evaluator.run(expressionNested, anyAccount.Id);
Assert.isTrue(nestedResult, 'There should be exactly one contact with the first name "First".');
}

@IsTest
static void hasPurchasedSomethingInThePast() {
List<CustomRecordContext> contexts = getContexts();

String pipedExpression = '@Customer.Assets -> WHERE(Product2Id = @Product.Id && Status = "Purchased") -> SIZE() > 0';
Boolean pipedResult = (Boolean)Evaluator.run(pipedExpression, contexts, new Configuration());
Assert.isTrue(pipedResult, 'The customer should have purchased the product in the past.');

String nestedExpression = 'SIZE(WHERE(@Customer.Assets, Product2Id = @Product.Id && Status = "Purchased")) > 0';
Boolean nestedResult = (Boolean)Evaluator.run(nestedExpression, contexts, new Configuration());
Assert.isTrue(nestedResult, 'The customer should have purchased the product in the past.');
}

private static List<CustomRecordContext> getContexts() {
Account someAccount = new Account(Name = 'Test Account');
insert someAccount;
Contact someone = new Contact(FirstName = 'Test', LastName = 'User', AccountId = someAccount.Id);
insert someone;
Product2 sampleProduct = new Product2(Name = 'Test Product', IsActive = true);
insert sampleProduct;

Asset sampleAsset = new Asset(
Name = 'Test Asset',
ContactId = someone.Id, Status = 'Purchased',
Product2Id = sampleProduct.Id);
insert sampleAsset;

CustomRecordContext customer = new CustomRecordContext('Customer', someone.Id);
CustomRecordContext product = new CustomRecordContext('Product', sampleProduct.Id);
List<CustomRecordContext> contexts = new List<CustomRecordContext> { customer, product };
return contexts;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>65.0</apiVersion>
<status>Active</status>
</ApexClass>