You need to sign in to do that
Don't have an account?

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;
}
}
}
}
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;
}
}
}
}
All Answers