Criteria - wimvelzeboer/fflib-apex-extensions GitHub Wiki

fflib-apex-extensions

Dynamic criteria filter

The criteria feature can be used as a common filter for domains and selectors, but it can also be used many other ways.

Formula based filtering

The formula based filtering allows you to even accept a (user provided) formula string setting the both the AND and OR conditions and the order in which they should be evaluated.

This can be useful if you want to give the user full control over creating their own conditions:

Example

Condition__c rule1 = new Condition__c( Field__c = 'FirstName',  Operator__c = 'Equals', Value__c = 'John');
Condition__c rule2 = new Condition__c( Field__c = 'LastName',   Operator__c = 'Equals', Value__c = 'Smith');
Condition__c rule2 = new Condition__c( Field__c = 'Department', Operator__c = 'Equals', Value__c = 'MIB');
List<Conditions__c> rules = new List<Conditions__c> { rule1, rule2, rule3 };

// Here we create the criteria and populate it with the rules
fflib_Citeria criteria = new fflib_Criteria();
for (Condition__c rule : rules)
{
   if (rule.Operator__c == 'Equals')
     criteria.equalTo(rule.Field__c, rule.Value__c);
}

criteria.FormulaCriteria('(1 OR 2) AND 3');

// Lets evaluate these records:
criteria.evaluate(new Contact(LastName = 'Smith', FirstName = 'John', Department = 'MIB'));  // true
criteria.evaluate(new Contact(LastName = 'Black', FirstName = 'John', Department = 'MIB'));  // true
criteria.evaluate(new Contact(LastName = 'Black', FirstName = 'Will', Department = 'MIB'));  // false
criteria.evaluate(new Contact(LastName = 'Smith', FirstName = 'John', Department = 'CIA'));  // false
⚠️ **GitHub.com Fallback** ⚠️