Fix bug in formula criteria

This commit is contained in:
Wim Velzeboer
2021-08-23 12:06:21 +01:00
parent 7e9c5a7fce
commit 19cb9c8809
2 changed files with 34 additions and 6 deletions
@@ -45,8 +45,6 @@ public virtual with sharing class fflib_Criteria
private String type; private String type;
private IFormulaEvaluator formulaEvaluator; private IFormulaEvaluator formulaEvaluator;
/** /**
* populate private variables with default settings. * populate private variables with default settings.
*/ */
@@ -173,6 +171,7 @@ public virtual with sharing class fflib_Criteria
return this; return this;
} }
private interface IFormulaEvaluator private interface IFormulaEvaluator
{ {
Boolean evaluate(Object record, Map<Integer, Boolean> evaluatedResultByNumber); Boolean evaluate(Object record, Map<Integer, Boolean> evaluatedResultByNumber);
@@ -384,12 +383,25 @@ public virtual with sharing class fflib_Criteria
private Boolean evaluateFormula(Object record) private Boolean evaluateFormula(Object record)
{ {
Map<Integer, Boolean> evaluatedResultByNumber = new Map<Integer, Boolean>(); try
for (Integer i = 0; i < evaluators.size(); i++)
{ {
evaluatedResultByNumber.put(i, evaluators.get(i).evaluate(record)); Map<Integer, Boolean> evaluatedResultByNumber = new Map<Integer, Boolean>();
for (Integer i = 0; i < evaluators.size(); i++)
{
evaluatedResultByNumber.put(i, evaluators.get(i).evaluate(record));
}
return formulaEvaluator.evaluate(record, evaluatedResultByNumber);
}
catch (Exception e)
{
// If an error occurs during the validation,
// just return a null value and send a warning message to the debug log;
System.debug(
System.LoggingLevel.WARN,
'Criteria condition failure with error: ' + e.getMessage() + '\n' + e.getStackTraceString()
);
return null;
} }
return formulaEvaluator.evaluate(record, evaluatedResultByNumber);
} }
/** /**
@@ -615,4 +615,20 @@ private with sharing class fflib_CriteriaTest
.evaluate(new Contact(LastName = 'Smith')) .evaluate(new Contact(LastName = 'Smith'))
); );
} }
@IsTest
static void itShouldEvaluateFormula_ReturnNullOnErrorInFormula()
{
SObject record = fflib_MockSObjectUtil.addRelatedParentObject(
new Contact(LastName = 'Smith'),
new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Industry = 'Energy'),
'Account');
System.assertEquals(null,
new fflib_Criteria()
.FormulaCriteria('1')
.equalTo('AccountTypo.Industry', 'Energy')
.evaluate(record)
);
}
} }