# UTIL_FormulaFilter

**Class** · Group: `Utilities`

Learn it: [Utilities guide](/utilities-guide)

<div class="apex-member apex-class">

```apex
global inherited sharing class UTIL_FormulaFilter
```

Class that can filter list of SObject based on Formula Information provided adapted from:apex-trigger-actions-framework

**Example**

```apex
UTIL_FormulaFilter filter = new UTIL_FormulaFilter(
    'MyProcess', 'UTIL_FormulaContext.AccountContext', 'TEXT(newRecord.Industry) == "Technology"'
);
UTIL_FormulaFilter.DTO_FilterResults results = filter.filter(oldRecords, newRecords);
```

</div>

---

## Methods

| Method | Description |
|--------|-------------|
| global [UTIL_FormulaFilter.DTO_FilterResults](/reference/apex/util-formulafilter-dto-filterresults) [filter](#filter)([List](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm)<[SObject](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm)> oldRecords, [List](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm)<[SObject](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm)> newRecords) | Filters the given lists of new and old SObjects based on the entry criteria formula. |

### filter

<div class="apex-member">

```apex
global UTIL_FormulaFilter.DTO_FilterResults filter(List<SObject> oldRecords, List<SObject> newRecords)
```

Filters the given lists of new and old SObjects based on the entry criteria formula.

This method evaluates the entry criteria formula for each record in the `triggerNew` and `triggerOld`.
If the formula evaluates to true for a record, it is included in the filtered lists.

When the context class also implements UTIL_ValidationRule.INT_BulkValidationContext, its preLoad method runs once per
call when there is at least one record, before any record is checked; on a delete it gets the deleted records first and null second.

*NOTE: Make sure that if both the old and new records lists are not null they are the same size*

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `oldRecords` | [List](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_list.htm) | The list of old SObjects to filter. |
| `newRecords` | [SObject](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_sobject.htm) | The list of new SObjects to filter. |

**Returns** [UTIL_FormulaFilter.DTO_FilterResults](/reference/apex/util-formulafilter-dto-filterresults) — An object containing the filtered lists of new and old SObjects.

**Throws**

| Exception | Description |
|-----------|-------------|
| [Exception](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_exception_methods.htm) | Exception could be thrown if the any of the formula build method's fail |

**Example**

```apex
DTO_FilterResults result = instance.filter(records, records);
```

</div>

## Constructors

| Constructor | Description |
|-------------|-------------|
| global [UTIL_FormulaFilter](#constructors)([String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) processName, [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) contextClassName, [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) formula) | Constructs a new instance with the formula details |

### UTIL_FormulaFilter(String processName, String contextClassName, String formula)

<div class="apex-member">

```apex
global UTIL_FormulaFilter(String processName, String contextClassName, String formula)
```

Constructs a new instance with the formula details

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `processName` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The name of the process calling the filter, will be used if an error is generated during process so logging will be specific |
| `contextClassName` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The class that will provide the relevant variables, context to the formula. |
| `formula` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The boolean formula to execute |

**Example**

```apex
UTIL_FormulaFilter instance = new UTIL_FormulaFilter('value', 'value', 'RecordType.Name = \'Default\'');
```

</div>

## Inner Classes

| Class | Description |
|-------|-------------|
| [DTO_FilterResults](/reference/apex/util-formulafilter-dto-filterresults) | Inner class representing the result of the filter method. |
| [INT_SObjectFormulaEvaluationContext](/reference/apex/util-formulafilter-int-sobjectformulaevaluationcontext) | Interface for providing context data to dynamic formula evaluations using Salesforce's FormulaEval namespace. |

---
