# How to create a Master selection custom rule - Advanced

## How to Use a Custom Formula for Complex Master Record Selection

In some cases, selecting a master record based on a simple value comparison (like highest number or checkbox = true) isn’t enough. When your business logic is more advanced, the best approach is to create a **custom formula field** that **scores each record** based on multiple conditions.

This score is then used to **sort the duplicate pair** and select the master record.

## When to Use a Formula-Based Master Rule

Use a formula when:

* You need to prioritize based on a combination of **picklists, metrics, and dates**
* You want to handle **tie-breaker logic** across several layers
* Your logic changes based on **status**, **recency**, or **business value**

## Step-by-Step Setup

### **1. Create a Custom Formula Field**

* Object: Account, Contact, or Lead (where duplicates occur)
* Field Type: Formula
* Return Type: Number
* Example API Name: `MyDuplicateMasterRecordScore__c`

### **2. Add Your Logic in the Formula Field**

Here’s a complete example based on business rules:

```salesforce
salesforceCopyEditCASE(
   Status__c,
   "Customer", 10000,      /* Rule 1: Always prioritize Customer */
   "Prospect", 5000,       /* Rule 3: Prospect over Target */
   "Target", 1000,
   0
) +
IF(
   AND(Status__c = "Customer", NOT(ISBLANK(Number_of_Opportunities__c))),
   Number_of_Opportunities__c * 10,  /* Rule 2: More Opps wins among Customers */
   0
) +
IF(
   AND(Status__c = "Prospect", Last_Touchpoint_Date__c < TODAY() - 180),
   -4000,  /* Penalize cold Prospects */
   0
) +
(100000000 - CreatedDate) /* Rule 4: Prefer oldest if all else equal */
```

This formula returns a numeric score. The record with the **highest score** becomes the master during the merge.

### **3. Use It in Delpha Duplicate Settings**

In **Delpha Setup → Duplicate Settings**, set the **Master Rule** to:

```
MyDuplicateMasterRecordScore__c DESC, CreatedDate ASC
```

#### What This Covers

* Rule 1: Always select **Customer** as master
* Rule 2: Among Customers, prioritize by **# of Opportunities**
* Rule 3: Prefer **Prospect** over **Target**, unless the Prospect is outdated
* Rule 4: Use **Created Date** as fallback


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.delpha.io/delpha-for-salesforce/how-to-faq/delpha-duplicate/how-to-create-a-master-selection-custom-rule-advanced.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
