Could you help me understand why does my duplicate rule is firing upon insert?
This is the controller:
Boolean idExists = false;
List < Lead > leads = [SELECT Id, Name FROM Lead WHERE Email =: email OR Company =: companyName OR Name =: name LIMIT 1];
idExists = leads.isEmpty() ? false : true;
try {
leads = createOrUpdateLead(leads, idExists, name, title, companyName, email, phone, companyId, companyRegNum, address);
} catch (Exception e) {
}
global static List < Lead > createOrUpdateLead(List < Lead > leads, Boolean idExists, String name, String title, String companyName, String email) {
Lead l = new Lead(
LastName = name,
Title = title,
Company = companyName,
Email = email
);
if (idExists) {
l.Id = leads[0].Id;
}
upsert l;
leads.add(l);
return leads;
}
The duplicate rule creteria is:
Lead: Email EXACT MatchBlank = FALSE
When the method is done executing, I get the following error:
Upsert failed. First exception on row 0 with id 00Q1l0000040kJFEAY; first error: DUPLICATES_DETECTED, This Lead is a duplicate. Please use the existing Lead: []
When I disable the rule and try again, I get another error (Standard Error I guess):
Upsert failed. First exception on row 0; first error: DUPLICATES_DETECTED, This Lead already exists as a Contact. Please use the existing Contact record.: []