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
Natasha Ali 3Natasha Ali 3 

Trigger to stop duplicate records into SF if email already exists on a person account

Hi,
I have an existing trigger which converts leads into peron accounts if the company name is blank.
I need to also write a trigger that stops records from coming into Salesforce if that email already exisits in a contact or an account (we use person accounts).
The issue is how do I get ti working with the existing trigger (I need it to run before the trigger that converts it into a person account is run). Is there a way to combine the two or add the functionality to create the record only if the email address doesn't already exist as a contact in the org. 
Below is the autoconvert trigger:
 
Trigger AutoConvert on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setDoNotCreateOpportunity(true);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}
Any help is MUCH appreciated!!
Many Thanks,
Natasha 
 
SATHISH REDDY.SATHISH REDDY.
Hi Natasha,

I see that there are coupl of ways to prevent records from creation. 
1. Use the native Duplicate management
2. Setup the custom validation using apex which would throw an error preventing record creation.
https://developer.salesforce.com/forums/?id=906F0000000BVQLIA4

Let me know if this helps.
Cheers!
Natasha Ali 3Natasha Ali 3
Hi Sathish, thanks for that. 
So we already have rules but they are not working properly and are unreliable (already spoke to SF about it). I need to add it into my trigger - I posted this on the stackexchaneg community too and got the following response:
 
Trigger AutoConvert on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     // Get Emails
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
     Set<string> leadEmails = new Set<string>();
     for(Lead lead: Trigger.new){
         if(! String.isBlank(lead.Email){
            leadEmails.add(lead.Email);
         }
     }
     Map<string, Contact> emails = new Map<string,Contact>([
         select 
            email 
         from 
            contact 
         where 
            Email IN :leadEmails
      ]);
      //Perform logic
     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Company == null) {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;

               lc.setLeadId(lead.Id);
               lc.setDoNotCreateOpportunity(true);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               // Check to see if the email exists
               if(!emails.containsKey(lc.Email__c){
                  leadConverts.add(lc);
               }
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

Is there anything else I would need to add? A separate handler class for the logic? How would I accomplish this?

Any help is much appreciated :)
Many Thanks,
Natasha