Newer Version Available
findDuplicatesByIds()
Syntax
1FindDuplicatesResult[] duplicateResults =
2 connection.findDuplicatesByIds(Id[] inputIdArray);Usage
Use findDuplicatesByIds() to apply duplicate rules associated with an object to records represented by the record IDs.
findDuplicatesByIds() uses the duplicate rules for the object that has the same type as the input record IDs. For example, if the record ID represents an Account, findDuplicatesByIds() uses the duplicate rules associated with the Account object.
For each input ID, findDuplicatesByIds() adds an object to the output array.
Matching is controlled by the values specified by the input record ID. The values can include a record ID only.
findDuplicatesByIds() searches the object defined by the duplicate rule for an existing record that has the same ID. Then it loads the values from that record, and searches for duplicates based on those values.
The output of findDuplicatesByIds() is an array of objects with the same number of elements as the input array, and in the same order. The output objects encapsulate record IDs for duplicate records. Optionally, the output objects also contain values from the duplicate records.
Each element contains a DuplicateResult object. If findDuplicatesByIds() doesn’t find any duplicates for an sObject, the duplicateRule field in DuplicateResult contains the name of the duplicate rule that findDuplicatesByIds() applied, but the matchResults array is empty.
If the includeRecordDetails flag in DuplicateRuleHeader is set to false, findDuplicatesByIds() returns only the record IDs of the matching records. Otherwise, findDuplicatesByIds() returns all the fields specified in the primary CompactLayout associated with the target object.
Basic Steps for Using
- Create one or more ID objects that correspond to the object that has the duplicate rules you want to use.
- Specify record IDs to compare to records in the object.
- Set DuplicateRuleHeader to control the output you want.
Sample
The following Java sample demonstrates how to search for duplicates of a Lead, using the standard Leads duplicate rule.
1package wsc;
2
3import com.sforce.soap.partner.*;
4import com.sforce.soap.partner.Error;
5import com.sforce.soap.partner.sobject.SObject;
6import com.sforce.ws.ConnectionException;
7import com.sforce.ws.ConnectorConfig;
8
9public class Main {
10
11 private static final String USERNAME = "YOUR-USERNAME";
12 private static final String PASSWORD = "YOUR-PASSWORD&SECURITY-TOKEN";
13 private static PartnerConnection connection = null;
14
15 public static void main(String[] args) throws ConnectionException {
16
17 // Create the configuration for the partner connection
18 ConnectorConfig config = new ConnectorConfig();
19 config.setUsername(USERNAME);
20 config.setPassword(PASSWORD);
21
22 // Initialize the connection
23 connection = new PartnerConnection(config);
24
25 SObject[] objectsToSearch = new SObject[2];
26 String[] inputIds = new String[2];
27 // Instantiate an empty Java SObject
28 SObject searchCriteria = new SObject();
29 // Set its type to Lead. This tells findDuplicatesByIds() to use the duplicate rules
30 // for Lead
31 searchCriteria.setType("Lead");
32 /*
33 * Set the necessary fields for matching, based on the standard matching rules for Lead
34 * (Search help.salesforce.com for "Standard Contact and Lead Matching Rule" to see the
35 * rules).
36 */
37 searchCriteria.setField("FirstName", "Marc");
38 searchCriteria.setField("LastName", "Benioff");
39 searchCriteria.setField("Company", "Salesforce.com Inc");
40 searchCriteria.setField("Title", "CEO");
41 searchCriteria.setField("Email", "ceo@salesforce.com");
42 // Add the sObjects to the input array
43 objectsToSearch[0] = searchCriteria;
44 objectsToSearch[1] = searchCriteria;
45
46 SaveResult[] saveResults = connection.create(objectsToSearch);
47
48 for (int i = 0; i < saveResults.length; ++i) {
49 if (saveResults[i].isSuccess()) {
50 System.out.println("Successfully created ID: " + saveResults[i].getId());
51 inputIds[i] = saveResults[i].getId();
52 } else {
53 System.out.println("Error: could not create SObject.");
54 System.out.println("The error reported was: " +
55 saveResults[i].getErrors()[0].getMessage() + "\n");
56 }
57 }
58 /*
59 * By default, findDuplicatesByIds() returns only record IDs. To return additional values,
60 * set the second parameter to true.
61 */
62 connection.setDuplicateRuleHeader(
63 /*
64 * @param allowSave - Not Applicable for this API call
65 */
66 false,
67 /* @param includeRecordDetails */
68 false,
69 /*
70 * @param runAsCurrentUser - Not Applicable for this API call
71 */
72 false);
73
74 // Invoke findDuplicatesByIds() to find duplicates based on the information in the
75 // SObject array
76 FindDuplicatesResult[] callResults = connection.findDuplicatesByIds(inputIds);
77
78 // Iterate through the results
79 /* For each Id in the input array, get the duplicate results. There could be more matches
80 * depending on the data in the organization.
81 */
82 for (FindDuplicatesResult findDupeResult : callResults) {
83 // If errors were found for this Id, print them out
84 if (!findDupeResult.isSuccess()) {
85 for (Error findDupError : findDupeResult.getErrors()) {
86 System.out.println("FindDuplicatesRule errors detected: " + findDupError.getMessage());
87 }
88 } else {
89 /*
90 * Get the DuplicateResult object array for the result. Each element in the array represents
91 * the result of testing one duplicate rule for the Id. Process each DuplicateResult.
92 */
93 for (DuplicateResult dupeResult : findDupeResult.getDuplicateResults()) {
94 System.out.println("Duplicate rule: " + dupeResult.getDuplicateRule());
95 // Print out the name of the object associated with the duplicate
96 // rule
97 System.out.println("Source of this duplicate rule is: " +
98 dupeResult.getDuplicateRuleEntityType());
99 for (MatchResult matchResult : dupeResult.getMatchResults()) {
100 if (!matchResult.isSuccess()) {
101 for (Error e : matchResult.getErrors()) {
102 System.out.println("Errors detected: " + e.getMessage());
103 }
104 } else {
105 System.out.println("Matching rule is: " + matchResult.getRule());
106 System.out.println("Object type for this matching rule is: " + matchResult.getEntityType());
107 for (MatchRecord matchRecord : matchResult.getMatchRecords()) {
108 System.out.println("Duplicate record ID: " + matchRecord.getRecord().getId());
109 }
110 }
111 }
112 }
113 }
114 }
115 }
116}Arguments
| Name | Type | Description |
|---|---|---|
| IDs | Array of ID | Required. A list of IDs that contain values you want to search for. |
Response
An array of FindDuplicatesResult objects.
FindDuplicatesResult
Represents the result of a duplicate search for a single ID in the input array. Because the object associated with the sObject can have more than one duplicate rule, FindDuplicatesResult contains an array of DuplicateResult objects.
Fields
| Field Name | Field Type | Description |
|---|---|---|
| duplicateResults | Array of DuplicateResult objects | The result of each duplicate rule applied by findDuplicatesByIds() to a single sObject. |
| errors | Array of Error objects | Contains an array of errors encountered by findDuplicatesByIds(). |
| success | boolean | This field is set to true if findDuplicatesByIds() doesn’t encounter any errors. |