> For the complete documentation index, see [llms.txt](https://help.delpha.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.delpha.io/delpha-for-salesforce/how-to-faq/delpha-duplicate/how-can-i-detect-what-record-is-the-master.md).

# How can I detect what record is the Master

Learn how to retrieve the surviving master record ID before updating and merging duplicate pairs using Delpha's Apex API.

When managing duplicate records, you often need a programmatic way to evaluate a pair of duplicates and determine which record should be preserved (the "Master") and which should be archived or deleted.

Depending on your use case, you can evaluate a single pair of duplicates or process them in bulk.

## 1. Single Record Processing

{% hint style="info" %}
**Overview:** If you are only evaluating a single duplicate pair at a time, you can use the `delpha.DDQ_DuplicateApi.getMaster()` method to determine which record will serve as the master (surviving) record prior to updating pair members and completing a duplicate merge.
{% endhint %}

### Method Overview

The function evaluates two duplicate records against your configured Delpha selection rules to identify the primary master record.

#### Function Signature

```apex
Id masterRecordId = delpha.DDQ_DuplicateApi.getMaster(
    '[RecordId1]', 
    '[RecordId2]', 
    '[DuplicateSettingId]'
);
```

#### Parameters

| **Parameter**        | **Type**      | **Description**                                                         |
| -------------------- | ------------- | ----------------------------------------------------------------------- |
| `RecordId1`          | `String / Id` | Salesforce Record ID of the first duplicate.                            |
| `RecordId2`          | `String / Id` | Salesforce Record ID of the second duplicate.                           |
| `DuplicateSettingId` | `String / Id` | The ID of the Delpha Duplicate Setting containing your selection rules. |

**Output**: Returns the `Id` of the master record.

### Step-by-Step Implementation Guide

#### Step 1: Select Duplicate Records

Query the duplicate pairs that require processing. This step is required when preparing records for bulk processing.

{% code overflow="wrap" %}

```sql
SELECT Id, delpha__RecordId__c, delpha__MatchedRecordId__c FROM delpha__DDQ_DuplicateRecord__c WHERE delpha__Processed__c = false AND (delpha__Status__c = 'Steward Yes' OR delpha__Status__c = 'Auto Yes')
```

{% endcode %}

#### Step 2: Retrieve the Duplicate Setting ID

1. Navigate to the Delpha Setup app in Salesforce.
2. Open the Duplicate Setting rule you wish to apply.
3. Copy the Record ID directly from the browser URL.

#### Step 3: Execute `getMaster`

Call `delpha.DDQ_DuplicateApi.getMaster()` passing `delpha__RecordId__c`, `delpha__MatchedRecordId__c`, and your `DuplicateSettingId`.

{% hint style="warning" %}
**Permissions**: The running user must have an active Delpha License and be assigned the Delpha Admin Permission Set Group.
{% endhint %}

## 2. Bulk Record Processing

When processing large volumes of duplicate records (for example, in a Scheduled Batch Apex job), you can resolve multiple pairs at once using a bulkified method.

### Method Overview: `resolve()`&#x20;

Evaluates a list of duplicate pairs and determines the winning master record for each pair in a single bulkified operation.

**Signature**

```apex
public static Map<Id, Resolution> resolve(List<delpha__DDQ_DuplicateRecord__c> pairs)
```

**Parameters**

| **Parameter** | **Type**                               | **Required** | **Description**                                                                                                                            |
| ------------- | -------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `pairs`       | `List<delpha__DDQ_DuplicateRecord__c>` | Yes          | List of duplicate pair SObjects to evaluate. Every record in the list must have all required fields explicitly queried in SOQL beforehand. |

**Return Value**

| **Return Type**       | **Description**                                                                                                                                                                                   |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Map<Id, Resolution>` | A map where each key corresponds to a duplicate pair record `Id` (`delpha__DDQ_DuplicateRecord__c.Id`), and the value is a `Resolution` object containing the master record determination result. |

**Key Behaviors & Exceptions**

* **No Automatic Field Check**: The method assumes all required fields are loaded. Omitting any required field from the input records will throw an unhandled `System.SObjectException`.
* **No Internal DML or Logging**: This method is purely evaluative—it returns calculation results and does not update records in the database or generate error logs internally.
* **1:1 Mapping**: Output guarantees exactly one `Resolution` entry per distinct duplicate pair ID passed in the `pairs` list.

### Step by Step implementation Guide&#x20;

#### Prerequisites

To execute this, you will use custom local Apex classes deployed in your Salesforce org to process the duplicate pairs generated by the Delpha managed package.

* `delpha__DDQ_DuplicateRecord__c`: The custom object provided by the Delpha package.
* `DuplicateResolver` and `Resolution`: Custom local Apex classes (not provided by Delpha) used to process Delpha's data and return the designated master record.

#### Code Example

Below is the standard snippet to retrieve unhandled duplicate pairs and resolve them in bulk.

```apex
// Note: DuplicateResolver and Resolution are custom local classes 
// (not provided by Delpha) used to process Delpha package data.

// 1. Query unresolved duplicate pairs from the Delpha managed package object
List<delpha__DDQ_DuplicateRecord__c> pairsToResolve = [
    SELECT Id, 
           delpha__RecordId__c, 
           delpha__MatchedRecordId__c, 
           delpha__IsCrossRecord__c, 
           delpha__DuplicateSettings__c
    FROM delpha__DDQ_DuplicateRecord__c
    WHERE delpha__Processed__c = false
    // you can also filter on the status (delpha__Status__c)
];

if (!pairsToResolve.isEmpty()) {
    // 2. Call your custom in-house resolution method in bulk
    Map<Id, Resolution> resolutionMap = DuplicateResolver.resolve(pairsToResolve);

    // 3. Process each resolved pair
    for (Id pairId : resolutionMap.keySet()) {
        Resolution res = resolutionMap.get(pairId);
        
        // Ensure the resolution evaluated successfully
        if (res != null && res.isSuccess()) {
            Id masterId = res.getMasterRecordId();
            System.debug('Pair ID: ' + pairId + ' -> Designated Master ID: ' + masterId);
            
            // --> Execute your custom merge (DML) or post-processing logic here <--
            
        } else if (res != null) {
            // Handle failed resolutions if needed
            System.debug(LoggingLevel.ERROR, 'Resolution failed: ' + res.getErrorMessage());
        }
    }
}
```

### Important Technical Considerations

* **Required Fields**

| **Field API Name**             | **Data Type** | **Description**                                                                                                        |
| ------------------------------ | ------------- | ---------------------------------------------------------------------------------------------------------------------- |
| `Id`                           | `Id`          | The unique Salesforce Record ID of the `delpha__DDQ_DuplicateRecord__c` duplicate pair record.                         |
| `delpha__RecordId__c`          | `Id` (Lookup) | The Salesforce Record ID of the primary record in the duplicate pair.                                                  |
| `delpha__MatchedRecordId__c`   | `Id` (Lookup) | The Salesforce Record ID of the matched candidate record in the duplicate pair.                                        |
| `delpha__IsCrossRecord__c`     | `Boolean`     | Indicates whether the match is across different SObject types (e.g., Lead vs. Contact) or within the same object type. |
| `delpha__DuplicateSettings__c` | `Id` (Lookup) | The ID of the specific Delpha duplicate setting rules/configuration applied to evaluate this pair.                     |

{% hint style="warning" %}
Omitting any of these 5 fields from your initial SOQL query will result in an unhandled `System.SObjectException` at runtime when `resolve()` executes.
{% endhint %}

* **The `Resolution` Map** \
  The custom method returns a `Map<Id, Resolution>` where the key is the ID of the `delpha__DDQ_DuplicateRecord__c` pair, and the value is a `Resolution` object containing the Master ID.
* **Next Steps** \
  The provided code evaluates the data, but does not execute any Database changes. You must replace the `System.debug()` line with your own Database operations (such as `Database.merge` or updating status fields) based on your business requirements.

{% hint style="warning" %}
**Permissions**: The running user must have an active Delpha License and be assigned the Delpha Admin Permission Set Group.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.delpha.io/delpha-for-salesforce/how-to-faq/delpha-duplicate/how-can-i-detect-what-record-is-the-master.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
