Fix bug in formula criteria
This commit is contained in:
@@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user