<?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>triggers | Taha Syed | Salesforce</title>
<atom:link href="https://www.syedtaha.com/tag/triggers/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>Mon, 03 Dec 2018 21:24:36 +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>Learn Salesforce Triggers with Examples and Scenarios</title><link>https://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Mon, 15 Oct 2018 20:41:31 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex triggers]]></category>
<category><![CDATA[beginners]]></category>
<category><![CDATA[examples]]></category>
<category><![CDATA[salesforce]]></category>
<category><![CDATA[scenarios]]></category>
<category><![CDATA[triggers]]></category>
<category><![CDATA[triggers with examples]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1072</guid><description><![CDATA[<p>Learn Salesforce Apex Triggers with examples and scenarios by dissecting them line by line. This List of Salesforce Triggers is for beginners to practice and learn.</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/">Learn Salesforce Triggers with Examples and Scenarios</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Learn Salesforce Triggers with Examples and Scenarios.</h1><p>&nbsp;</p><p>This post is dedicated to learning Salesforce triggers with examples and scenarios by dissecting them line by line. All triggers will be written with plenty of comments and explanations so that its easier for us to understand and implement.</p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/" target="_blank" rel="noopener"><strong>Write Your First Hello World Salesforce Apex Trigger – Triggers for Beginners</strong></a></li></ul><p
style="text-align: left; padding-left: 30px;"><em>The <strong>“Hello World”</strong> program is the simplest program used to illustrate the basic syntax of a programming language. The program outputs “Hello World” and is traditionally the first program written by all beginner developers. Our <strong>Hello World </strong>trigger&#8217;s requirement is: <strong>When a Lead is updated, update the lead’s first name to ‘Hello’ and the last name to ‘World’</strong></em></p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/" target="_blank" rel="noopener"><strong>Trigger to Change the Stage when an Opportunity is created</strong></a></li></ul><p
style="padding-left: 30px;"><em>Every time an Opportunity is created (and the user selects any Stage), the <strong>Opportunity Stage</strong> should change and default to <strong>Prospecting. </strong></em></p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/" target="_blank" rel="noopener"><strong>Trigger to create a task when an Opportunity is updated</strong></a></li></ul><p
style="padding-left: 30px;"><em>When an Opportunity is updated, a new <strong>Task</strong> should be <strong>created</strong> and assigned to the <strong>Opportunity Owner</strong>. The status of the task should be &#8220;In Progress&#8221;</em></p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/trigger-to-roll-up-summary-contacts-field-to-accounts/1016/" target="_blank" rel="noopener"><strong>Trigger to create a roll-up summary of Contact&#8217;s fields on Accounts</strong></a></li></ul><p
style="padding-left: 30px;"><em>On each Account, the Total Salary custom field should hold the sum of Salaries from its associated Contacts. The trigger should work whenever a Contact’s Salary is updated, New Contact is Created or when an existing Contact is deleted.</em></p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/salesforce-apex-trigger-to-prevent-duplicate-contacts-by-email-or-phone-number/897/" target="_blank" rel="noopener"><strong>Trigger to Prevent Duplicate Contacts</strong></a></li></ul><p
style="text-align: left; padding-left: 30px;"><em>When a <strong>Contact</strong> is being <strong>created</strong> and if a record with the same <strong>Email Id or Phone number exists</strong>, the <strong>trigger</strong> should throw an <strong>error</strong> and the <strong>User</strong> should <strong>not</strong> be <strong>allowed</strong> to <strong>create</strong> the <strong>Duplicate Contact</strong></em></p><p>&nbsp;</p><ul><li><a
href="http://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/" target="_blank" rel="noopener"><strong>Trigger to Post Automated Random Chatter Comment</strong></a></li></ul><p
style="padding-left: 30px;">When a user posts to chatter and if the user&#8217;s post contains the term &#8220;Success&#8221;, an automated comment should be posted. This comment should be randomly selected from a list of comments; And should be posted as a specific user.</p><p
style="padding-left: 30px;"><em><strong> </strong></em></p><p>&nbsp;</p><p>Please support this post by linking it from your website/blog<br
/>
<a
href="http://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/">http://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/</a></p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/learn-salesforce-triggers-with-examples-and-scenarios/1072/">Learn Salesforce Triggers with Examples and Scenarios</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/learn-salesforce-triggers-with-examples-and-scenarios/1072/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item><title>Trigger to create task when Opportunity is updated &#8211; Salesforce Apex Triggers</title><link>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Wed, 25 Jul 2018 19:57:06 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex]]></category>
<category><![CDATA[create task]]></category>
<category><![CDATA[opportunity]]></category>
<category><![CDATA[salesforce trigger]]></category>
<category><![CDATA[trigger on opportunity]]></category>
<category><![CDATA[triggers]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=958</guid><description><![CDATA[<p>Salesforce Apex Trigger to create a Task when an Opportunity is updated. The task should be assigned to the Opportunity owner and the status of the task should be "In Progress". - Salesforce Apex Trigger Examples</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/">Trigger to create task when Opportunity is updated – Salesforce Apex Triggers</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Trigger to create a task when an Opportunity is updated &#8211; Salesforce Apex Triggers for Beginners.</h1><p><strong>Requirement: </strong>Write a Salesforce Apex Trigger to create a Task when an Opportunity is updated. The task should be assigned to the Opportunity owner and the status of the task should be &#8220;In Progress&#8221;.</p><p><strong>The Salesforce Apex Trigger Sytax is as follows:</strong></p><pre class="lang:default decode:true" title="Apex Trigger Syntax">trigger *triggerName* on *Object* (*before/after event*) {
    for (*Object* *Variable* : Trigger.New)      
        **Trigger Actions &amp; Code Block**
}</pre><p>&nbsp;</p><p>Now lets write our trigger with the following steps.</p><ol><li> Define the trigger</li><li> Iterate through each opportunity</li><li> Create a Task</li><li> Assign values to the Task</li></ol><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></ul><div
class="tab_container"><div
id="tab-0-step-1" class="tab_content clearfix"><br
/>
////define the trigger name, object and events</p><pre class="lang:default decode:true">trigger createTaskOnOpp on Opportunity (before update) {</pre><p></div><div
id="tab-1-step-2" class="tab_content clearfix"><br
/>
//Iterate through each opportunity and assign it the variable Opp</p><pre class="lang:default decode:true ">for (Opportunity Opp :Trigger.New) {</pre><p></div><div
id="tab-2-step-3" class="tab_content clearfix"><br
/>
//Create a new task and assign it the variable <strong>t</strong></p><pre class="lang:default decode:true ">Task t = new Task();</pre><p></div><div
id="tab-3-step-4" class="tab_content clearfix"></p><p>//Assign Values to the task <strong>t</strong></p><pre class="lang:default decode:true">	t.WhatID = Opp.Id;
	t.Ownerid = opp.Ownerid;
	t.subject = 'This is the Subject';
	t.Status = 'In Progress';
	t.Description = 'This is the Description';
	insert t;
	}
}</pre><p></div></div></div><div
class="clear"></div><p>The complete Salesforce Apex Trigger to create a Task when an Opportunity is updated would be:</p><pre class="lang:default decode:true">////define the trigger name, object and events
trigger createTaskOnOpp on Opportunity (before update) {
    //Iterate through each opportunity and assign it the variable Opp
    for (Opportunity Opp :Trigger.New) {
	//Create a new task and assign it the variable t
        Task t = new Task();
        // Assign Values to the task t
        t.WhatID          = Opp.Id;
        t.Ownerid         = opp.Ownerid;
        t.subject 	  = 'This is the Subject';
        t.Status	  = 'In Progress';
        t.Description     = 'This is the Description';
        insert t;
    }
}</pre><p>&nbsp;</p><p>This is how the trigger looks in the developer console (image)</p><figure
id="attachment_976" aria-describedby="caption-attachment-976" style="width: 818px" class="wp-caption alignnone"><img
fetchpriority="high" decoding="async" class="wp-image-976 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-create-Task-when-Opportunity-is-updated.png" alt="Trigger to create Task when Opportunity is updated" width="818" height="514" srcset="https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-create-Task-when-Opportunity-is-updated.png 818w, https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-create-Task-when-Opportunity-is-updated-300x189.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-create-Task-when-Opportunity-is-updated-768x483.png 768w" sizes="(max-width: 818px) 100vw, 818px" /><figcaption
id="caption-attachment-976" class="wp-caption-text">Trigger to create Task when Opportunity is updated</figcaption></figure><p>&nbsp;</p><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/">Trigger to create task when Opportunity is updated – Salesforce Apex Triggers</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/trigger-to-create-task-when-opportunity-is-updated-salesforce-apex-triggers/958/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item><title>Write Your First Hello World Salesforce Apex Trigger &#8211; Triggers for Beginners</title><link>https://www.syedtaha.com/salesforce-apex-triggers/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Tue, 03 Jul 2018 20:57:23 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex]]></category>
<category><![CDATA[apex trigger]]></category>
<category><![CDATA[beginners triggers]]></category>
<category><![CDATA[lead]]></category>
<category><![CDATA[salesforce trigger]]></category>
<category><![CDATA[triggers]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=946</guid><description><![CDATA[<p>Let's write our first "Hello World" Salesforce Apex trigger which will update a Lead with "Hello" as the first name and World" as the last name.<br
/>
The "Hello World" program is the simplest program used to illustrate the basic syntax of a programming language. The program outputs "Hello World" and is traditionally the first program written by all beginner developers.</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/">Write Your First Hello World Salesforce Apex Trigger – Triggers for Beginners</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Apex Triggers for Beginners</h1><p>The &#8220;Hello World&#8221; program is the simplest program used to illustrate the basic syntax of a programming language. The program outputs &#8220;Hello World&#8221; and is traditionally the first program written by all beginner developers.</p><p>Let&#8217;s write our first &#8220;Hello World&#8221; Salesforce Apex trigger which will update a Lead with &#8220;Hello&#8221; as the first name and &#8220;World&#8221; as the last name.</p><p><strong>Requirement:</strong><br
/>
When a Lead is updated, update the lead&#8217;s first name to &#8216;Hello&#8217; and last name to &#8216;World&#8217;</p><p>Lets write the trigger in the &#8220;Developer Console&#8221;. To access the developer console, click the drop down on your <strong>Name</strong> (on the top right), and then click on <strong>Developer Console.</strong></p><figure
id="attachment_950" aria-describedby="caption-attachment-950" style="width: 1024px" class="wp-caption alignnone"><img
decoding="async" class="wp-image-950 size-large" src="http://www.syedtaha.com/wp-content/uploads/2018/07/Salesforce-Developer-Console-1024x185.png" alt="Salesforce Developer Console" width="1024" height="185" srcset="https://www.syedtaha.com/wp-content/uploads/2018/07/Salesforce-Developer-Console-1024x185.png 1024w, https://www.syedtaha.com/wp-content/uploads/2018/07/Salesforce-Developer-Console-300x54.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/07/Salesforce-Developer-Console-768x139.png 768w, https://www.syedtaha.com/wp-content/uploads/2018/07/Salesforce-Developer-Console.png 1551w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption
id="caption-attachment-950" class="wp-caption-text">Salesforce Developer Console</figcaption></figure><p>&nbsp;</p><p>Once the developer console is open, click on <strong>File &gt;&gt; Open &gt;&gt; Apex Trigger. </strong>Now we are ready to write our first trigger.</p><figure
id="attachment_951" aria-describedby="caption-attachment-951" style="width: 560px" class="wp-caption aligncenter"><img
decoding="async" class="wp-image-951 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/07/Open-Apex-Trigger-in-Developer-Console.png" alt="Open Apex Trigger in Developer Console" width="560" height="421" srcset="https://www.syedtaha.com/wp-content/uploads/2018/07/Open-Apex-Trigger-in-Developer-Console.png 560w, https://www.syedtaha.com/wp-content/uploads/2018/07/Open-Apex-Trigger-in-Developer-Console-300x226.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/07/Open-Apex-Trigger-in-Developer-Console-320x240.png 320w" sizes="(max-width: 560px) 100vw, 560px" /><figcaption
id="caption-attachment-951" class="wp-caption-text">Open Apex Trigger in Developer Console</figcaption></figure><p>&nbsp;</p><p><strong>The Trigger Syntax:</strong><br
/>
Every trigger is defined by using the following syntax:</p><pre class="top-margin:20 bottom-margin:20 left-margin:20 right-margin:20 lang:go range:1-10 decode:true">  trigger *triggerName* on *Object* (*before/after event*) {
       for (*Object* *Variable* : Trigger.New)
       **Trigger Actions &amp; Code Block**
  }</pre><p>&nbsp;</p><p>Here is what the terms in the syntax mean:</p><ul><li>&#8216;triggerName&#8217; is the name of the trigger which describes it.</li><li>&#8216;Object&#8217; is the name of the Object on which the trigger is created</li><li>&#8216;before/after event&#8217; are a comma separated list of events like insert, update, delete</li><li>&#8216;Trigger.New&#8217; is a list of sObject records</li><li>&#8216;Variable&#8217; denote a piece of memory that can contain a data value. Descriptive text is used as variables.</li></ul><p>Lets get back to our requirement and based on the above syntax, write our <strong>&#8216;Hello World&#8217; Salesforce Apex trigger</strong>.<br
/>
Remember, when a <strong>Lead</strong> is <strong>updated</strong>, the <strong>first nam</strong>e should be updated to &#8220;<strong>Hello</strong>&#8221; and the <strong>Last Name</strong> to &#8220;<strong>World</strong>&#8220;.</p><p>We will be using <strong>comments</strong> to <strong>describe the lines of code</strong> in our <strong>trigger</strong>. But what are comments? <strong>Comments are used to describe code</strong> and are ignored by the parser &#8211; meaning the comments are NOT treated as code and will not run.<br
/>
Single line comments are preceded by // and multi line comments are wrapped in /* and */</p><p>Now here comes the trigger:</p><pre class="lang:default decode:true">// Name of the trigger is HelloWorld, It is on the Lead Object and It will run before the lead is updated (event).
trigger HelloWorld on Lead (before update) {
        //for every lead record, denoted by variable ld,in the list that is being updated
        for (lead ld : Trigger.new) {
             // add the first name as 'Hello'
             ld.FirstName = 'Hello';
             // add the last name as 'World'
             ld.LastName = 'World';
         }
}</pre><p>&nbsp;</p><p>This is how it looks in the developer console.</p><figure
id="attachment_955" aria-describedby="caption-attachment-955" style="width: 726px" class="wp-caption alignnone"><img
loading="lazy" decoding="async" class="wp-image-955 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/07/Hello-World-Salesforce-Apex-Trigger-1.png" alt="Hello World Salesforce Apex Trigger" width="726" height="354" srcset="https://www.syedtaha.com/wp-content/uploads/2018/07/Hello-World-Salesforce-Apex-Trigger-1.png 726w, https://www.syedtaha.com/wp-content/uploads/2018/07/Hello-World-Salesforce-Apex-Trigger-1-300x146.png 300w" sizes="auto, (max-width: 726px) 100vw, 726px" /><figcaption
id="caption-attachment-955" class="wp-caption-text">Hello World Salesforce Apex Trigger</figcaption></figure><p>&nbsp;</p><p><strong>Exercise to get used to the syntax and the basic trigger for beginners:</strong></p><p>Write a trigger to update the first name of the contact as Taha and the last name as Syed and the Phone number as 000-111-9999</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/">Write Your First Hello World Salesforce Apex Trigger – Triggers for Beginners</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/write-your-first-hello-world-salesforce-apex-trigger-triggers-for-beginners/946/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<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
loading="lazy" 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="auto, (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>