function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steven Wellman 28Steven Wellman 28 

duplicate value found: <unknown> duplicates value on record with id: <unknown> on line 13

I'm trying to write a trigger that looks for matching leads:
trigger DedupLeadLead on Lead (before insert) {

        // Get the data quality queue record ready for future use
        List<Group> dataQualityGroups = [SELECT Id
                                          FROM Group
                                         WHERE DeveloperName = 'Data_Quality'
                                         LIMIT 1];

        for (Lead myLead : Trigger.new) {
            if (myLead.Email != null) {

            // Searching for matching leads
            List<Lead> matchingLeads = [SELECT Id,
                                               FirstName,
                                               LastName,
                                               Company
                                          FROM Lead
                                         WHERE Company = :myLead.Company
                                        ];

            System.debug(matchingLeads.size() + ' lead(s) found.');

            // If matches are found...
            if (!matchingLeads.isEmpty()) {

                // Assign the lead to the data quality queue
                if (!dataQualityGroups.isEmpty()) {
                    myLead.OwnerId = dataQualityGroups.get(0).Id;
                }
                

                // Add the dupe contact ID's into the lead description
                String dupeLeadsMessage = 'Duplicate lead(s) found:\n';
                for (Lead matchingLead : matchingLeads) {
                    dupeLeadsMessage += matchingLead.FirstName + ' '
                                         + matchingLead.LastName + ', '
                                         + matchingLead.Company + ' ('
                                         + matchingLead.Id + ')\n';    
                }
                if (myLead.Description != null) {
                    dupeLeadsMessage += '\n' + myLead.Description;
                }
                
                myLead.Description = dupeLeadsMessage;
            }
        }
    }
}
Best Answer chosen by Steven Wellman 28
Raj VakatiRaj Vakati
The issue is not with your code .. looks like you have a data matching rules or duplicate rules are enabled in your org .. please deactivate them and test it 

All Answers

Steven Wellman 28Steven Wellman 28
Line 13 is: List<Lead> matchingLeads = [SELECT Id,
Raj VakatiRaj Vakati
The issue is not with your code .. looks like you have a data matching rules or duplicate rules are enabled in your org .. please deactivate them and test it 
This was selected as the best answer
Steven Wellman 28Steven Wellman 28
@Raj, that was it! Thank you!!