1

I have the following code that checks Phone, Phone2 and Mobile for Leads and gives an error if a Lead with the same phone exists, before saving the new record. How do I turn it into a code that will give me the same error if a Contact has the same phone number (the Lead would be a duplicate of the existing Contact)?

    trigger Lead_Phone_Dup on Lead (before insert, before update){

Map<String, Lead> leadMap =new Map<String, Lead>();
for (Lead lead : System.Trigger.new)
{   
 if ((lead.Phone !=null) &&
       (System.Trigger.isInsert ||
         (lead.Phone != System.Trigger.oldMap.get(lead.Id).Phone)))
    {
        lead.Phone= lead.Phone.replaceAll('[^0-9,+,/]', '');
     if (leadMap.containsKey(lead.Phone))
        {
            lead.Phone.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
            lead.Phone_2__c.addError('');
            lead.MobilePhone.addError('');
        }else{
            leadMap.put(lead.Phone, lead);
        }
   }



  if ((lead.Phone_2__c !=null) &&
       (System.Trigger.isInsert ||
         (lead.Phone_2__c != System.Trigger.oldMap.get(lead.Id).Phone_2__c)))
    {
        lead.Phone_2__c= lead.Phone_2__c.replaceAll('[^0-9,+,/]', '');
     if (leadMap.containsKey(lead.Phone_2__c))
        {
            lead.Phone.addError('');
            lead.Phone_2__c.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
            lead.MobilePhone.addError('');
        }else{
            leadMap.put(lead.Phone_2__c, lead);
        }
   }   

 if ((lead.MobilePhone !=null) &&
       (System.Trigger.isInsert ||
         (lead.MobilePhone != System.Trigger.oldMap.get(lead.Id).MobilePhone)))
    {
     lead.MobilePhone= lead.MobilePhone.replaceAll('[^0-9,+,/]', '');
     if (leadMap.containsKey(lead.MobilePhone))
        {
            lead.Phone.addError('');
            lead.Phone_2__c.addError('');
            lead.MobilePhone.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
        }else{
            leadMap.put(lead.MobilePhone, lead);
        }
   }
}


for (Lead lead : [SELECT Phone FROM Lead
                  WHERE Phone IN :leadMap.KeySet()])
{
    Lead newLead = leadMap.get(lead.Phone);
    newLead.Phone.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
    newLead.Phone_2__c.addError('');
    newLead.MobilePhone.addError('');
}

for (Lead lead : [SELECT Phone_2__c FROM Lead
                  WHERE Phone_2__c IN :leadMap.KeySet()])
{
    Lead newLead = leadMap.get(lead.Phone_2__c);
    newLead.Phone.addError('');
    newLead.Phone_2__c.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
    newLead.MobilePhone.addError('');
}

for (Lead lead : [SELECT MobilePhone FROM Lead
                  WHERE MobilePhone IN :leadMap.KeySet()])
{
    Lead newLead = leadMap.get(lead.MobilePhone);
    newLead.Phone.addError('');
    newLead.Phone_2__c.addError('');
    newLead.MobilePhone.addError('Duplicate warning: This or another existing Lead has the same Phone/Mobile number.');
  }
}
0

I found the solution. I have created separate Duplicate Rules for each set of fields.

For example:

  • 1 rule that compares Email(Lead) with Email (Contact).
  • and 1 rule that compares Email (Lead) with Email2 (Contact).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.