# How to create a Master selection custom rule

## How Master Record Selection Works in Delpha

When merging duplicate records, Delpha determines the **Master Record** using a defined **sorting rule**. The selected master record is the one from which field values are retained during the merge, unless overridden.

This article explains how to configure the **Master Record selection rule**, including available sorting options, supported field types, and fallback mechanisms.

## Sorting Options

You can define how records in a duplicate pair are sorted using the following syntax in your **Delpha Duplicate Settings**:

**➕ `ASC` or `DESC`**

* `ASC` (default): Sorts in ascending order.
* `DESC`: Sorts in descending order.

**➕ `NULLS FIRST` or `NULLS LAST`**

* `NULLS FIRST`: Prioritizes records with null values.
* `NULLS LAST` (default): Prioritizes records with filled values.

#### Sorting and Data Types

* **Picklists**: Sorted using the picklist order defined in Salesforce Setup.
* **Phone Fields**: Sorted alphanumerically, ignoring formatting characters (dashes, parentheses).
* **Currency Fields**: Sorted using the corporate currency value.

> ⚠️ **Not supported fields:**\
> Multi-select picklists, Rich text areas, Long text areas, and Encrypted fields (if enabled).

Reference: [Salesforce SOQL ORDER BY](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm)

## Examples

### **Example 1: Based on a numeric custom field**

```plaintext
NBOfB2COpportunities__c DESC
```

Select the record with the **highest number of B2C opportunities**.

### **Example 2: Based on checkbox field**

```plaintext
IsActive DESC
```

Select the record that is **active** (`True` = 1, `False` = 0).

***

## Fallback Mechanism

Always define **fallback rules** to handle tie-breaker scenarios.

Use a **comma-separated list** to define sorting order:

```plaintext
NBOfB2COpportunities__c DESC, IsActive DESC, CreatedDate ASC
```

Explanation:

1. Select the record with the **most B2C opportunities**.
2. If tied, choose the one marked **active**.
3. If still tied, select the one with the **oldest creation date**.

***

## Summary Table

| Sort Type     | Use Case                           |
| ------------- | ---------------------------------- |
| `ASC`         | Lowest value first                 |
| `DESC`        | Highest value first                |
| `NULLS FIRST` | Prioritize records missing a value |
| `NULLS LAST`  | Default — prioritize filled values |
| `,`           | Use to create fallback rule chains |
