<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>duplicate contacts | Taha Syed | Salesforce</title>
<atom:link href="https://www.syedtaha.com/tag/duplicate-contacts/feed/" rel="self" type="application/rss+xml" /><link>https://www.syedtaha.com</link>
<description>Taha Syed &#124; Sales to Salesforce &#124; Dissecting and Defining the Rationale..</description>
<lastBuildDate>Thu, 23 Aug 2018 21:41:43 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>
hourly	</sy:updatePeriod>
<sy:updateFrequency>
1	</sy:updateFrequency>
<generator>https://wordpress.org/?v=6.9.4</generator>
<item><title>Trigger to Prevent Duplicate Contacts Using Email or Phone Number</title><link>https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Mon, 11 Jun 2018 20:41:28 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[duplicate contacts]]></category>
<category><![CDATA[duplicate management]]></category>
<category><![CDATA[prevent duplicates]]></category>
<category><![CDATA[triggers]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=897</guid><description><![CDATA[<p>Salesforce Apex Trigger to Prevent the user from creating Duplicate Contacts when a Contact already exists with the same Email or Phone number - Triggers for Beginners</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/">Trigger to Prevent Duplicate Contacts Using Email or Phone Number</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Salesforce Apex Trigger to Prevent the user from creating Duplicate Contacts when a Contact already exists with the same Email or Phone number &#8211; Triggers for Beginners</h1><h2>The requirement is:</h2><p>When a Contact is being created, if a record with the same Email or Phone number exists, the <strong>trigger</strong> should throw an error and the user should <strong>not</strong> be allowed to create the duplicate Contact.</p><p>To achieve this, we will write a trigger with the following flow:</p><ol><li>Define the Trigger</li><li>Create <strong>Sets</strong> to store the &#8216;Email Ids&#8217; and &#8216;Phone Numbers&#8217; of all the records that are being created</li><li>Iterate through each record that is being created, and add their &#8216;Email Ids&#8217; and &#8216;Phone Numbers&#8217; to the <strong>Sets</strong>.</li><li>Create a <strong>List</strong> to store the &#8216;Email Ids&#8217; and &#8216;Phone Numbers&#8217; of existing records that match the records being created.</li><li>Populate the above <strong>List </strong>with of existing records whose email id or phone number matches the ones being created.</li><li>Iterate through each record being created and display and error if the record already exists.</li></ol><p>Now lets go through each step once again and write its respective code.</p><div
class="tab_widget wp_shortcodes_tabs"><ul
class="wps_tabs"><li><a
href="#" data-tab="tab-0-step-1">Step 1</a></li><li><a
href="#" data-tab="tab-1-step-2">Step 2</a></li><li><a
href="#" data-tab="tab-2-step-3">Step 3</a></li><li><a
href="#" data-tab="tab-3-step-4">Step 4</a></li><li><a
href="#" data-tab="tab-4-step-5">Step 5</a></li><li><a
href="#" data-tab="tab-5-step-6">Step 6</a></li></ul><div
class="tab_container"><div
id="tab-0-step-1" class="tab_content clearfix"></p><h2>1. Defining the trigger (the trigger syntax)</h2><p>Lets name the trigger as "<strong>PreventDuplicateContacts</strong>". The object name would be "<strong>Contact</strong>" and the trigger event would be "<strong>before insert"</strong> as we have to check if a duplicate record exists and display the error <strong>before</strong> the <strong>Contact</strong> is <strong>created.</strong></p><pre class="lang:default decode:true ">trigger PreventDuplicateContacts on Contact (before insert) {

}</pre><p>&nbsp;</p><p></div><div
id="tab-1-step-2" class="tab_content clearfix"></p><h2>2. Create Sets</h2><p>In this step, we would be creating two <strong>Sets</strong> of data type <em>String</em> to hold Email ids and Phone numbers of records that are being created. Lets call these Sets <strong>emailSet</strong> and <strong>phoneSet </strong>respectively</p><pre class="lang:default decode:true">Set &lt;String&gt; emailSet = new Set&lt;String&gt;();
Set &lt;String&gt; phoneSet = new Set&lt;String&gt;();</pre><p></div><div
id="tab-2-step-3" class="tab_content clearfix"></p><h2>3. Iterate through records being created</h2><p>Here we will iterate through each Contact record that is being created. During iteration, we will add the email id and phone number of each Contact  to their Sets.</p><pre class="lang:default decode:true ">for (contact con:trigger.new) {
     emailSet.add(con.email);
     phoneSet.add(con.phone);
}</pre><p>&nbsp;</p><p></div><div
id="tab-3-step-4" class="tab_content clearfix"></p><h2>4. Create a List</h2><p>We will now create a blank List named contactList. This List will be used in the next step to hold Contact Id of existing Contacts.</p><pre class="lang:default decode:true ">// New list to store the found email or phone numbers
List &lt;Contact&gt; contactList = new List&lt;Contact&gt;();
</pre><p>&nbsp;</p><p></div><div
id="tab-4-step-5" class="tab_content clearfix"></p><h2>5. Populate the list with existing records</h2><p>Using SOQL. the above List is now being populated with the <strong>Id </strong>of existing records, whose "Email or Phone number" matches the "Email or Phone number" of the records being created. As the Email and Phone of the records being created was stored in the Sets in Step 2, those sets are being used as reference in SOQL.</p><pre class="lang:default decode:true ">contactList = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];</pre><p>&nbsp;</p><p></div><div
id="tab-5-step-6" class="tab_content clearfix"></p><h2>6. Iterate through Records and Display Error.</h2><p>Now iterate through each record that is being created and display an Error on the Email field. If the Size of the list is greater than zero, it will mean that the existing record was found.</p><pre class="lang:default decode:true ">for (contact con:trigger.new) {
     If(contactList.size() &gt; 0) {
     con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' ); // Displaying the error
     }
}</pre><p>&nbsp;</p><p></div></div></div><div
class="clear"></div><p>The complete trigger to prevent duplicate contacts by matching email or phone number and displaying the error on the email field would be:</p><pre class="lang:default decode:true">trigger PreventDuplicateContacts on Contact (before insert) {
    
    // Set to store email ids
    Set &lt;String&gt; emailSet = new Set&lt;String&gt;(); 
    // Set to store phone numbers
    Set &lt;String&gt; phoneSet = new Set&lt;String&gt;(); 
    
    // Iterate through each Contact and add their email and phone number to their respective Sets
    for (contact con:trigger.new) {
        emailSet.add(con.email);
        phoneSet.add(con.phone);
    }

    // New list to store the found email or phone numbers
    List &lt;Contact&gt; contactList = new List&lt;Contact&gt;();

    // Populating the list using SOQL
    contactlist = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];

    // Iterating through each Contact record to see if the same email or phone was found
    for (contact con:trigger.new) {
        If (contactList.size() &gt; 0) {
            // Displaying the error
            con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
        }
    }

}</pre><p>&nbsp;</p><p>Here is how the <strong>Trigger to Prevent Duplicate Contacts</strong> looks in the Salesforce Developer Console (image)</p><figure
id="attachment_971" aria-describedby="caption-attachment-971" style="width: 733px" class="wp-caption alignnone"><img
fetchpriority="high" decoding="async" class="wp-image-971 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/06/Salesforce-Apex-Trigger-to-Prevent-Duplicate-Contacts-For-Beginners.png" alt="Salesforce Apex Trigger to Prevent Duplicate Contacts - For Beginners" width="733" height="461" srcset="https://www.syedtaha.com/wp-content/uploads/2018/06/Salesforce-Apex-Trigger-to-Prevent-Duplicate-Contacts-For-Beginners.png 733w, https://www.syedtaha.com/wp-content/uploads/2018/06/Salesforce-Apex-Trigger-to-Prevent-Duplicate-Contacts-For-Beginners-300x189.png 300w" sizes="(max-width: 733px) 100vw, 733px" /><figcaption
id="caption-attachment-971" class="wp-caption-text">Salesforce Apex Trigger to Prevent Duplicate Contacts &#8211; For Beginners</figcaption></figure><p>&nbsp;</p><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/">Trigger to Prevent Duplicate Contacts Using Email or Phone Number</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></content:encoded>
<wfw:commentRss>https://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
</channel>
</rss>