<?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>Taha Syed | Salesforce</title>
<atom:link href="https://www.syedtaha.com/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, 17 Dec 2018 20:19:16 +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 Post Automated Random Chatter Comment</title><link>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Mon, 03 Dec 2018 20:45:03 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex]]></category>
<category><![CDATA[chatter]]></category>
<category><![CDATA[comment]]></category>
<category><![CDATA[feedcomment]]></category>
<category><![CDATA[feeditem]]></category>
<category><![CDATA[post]]></category>
<category><![CDATA[salesforce]]></category>
<category><![CDATA[trigger]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1099</guid><description><![CDATA[<p>Apex Trigger to post an automated random comment whenever a chatter post is published. When any user posts to chatter and if the user's post contains the term "Success", 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>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/">Trigger to Post Automated Random Chatter Comment</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<p><strong>Requirement: </strong>Write a trigger to post an automated random comment whenever a chatter post is published.<br
/>
<strong>Use Case:</strong> When any 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>To achieve this requirement, we will be using two Chatter objects viz. <strong>Feed Item</strong> and <strong>Feed Comment. &#8216;</strong>Feed Item&#8217; records are the posts created by a User and the &#8216;Feed Comment&#8217; records are the comments on that post.</p><p>In other words, our requirement is to <strong>insert a random &#8216;Feed Comment&#8217; record, whenever a &#8216;Feed Item&#8217; record is inserted.</strong></p><p>The Trigger should be written with the following flow:</p><ol><li>Define the Trigger</li><li>Create a list for DML (last step)</li><li>Create a List of &#8216;Strings&#8217;/ Comments</li><li>Get a Random comment from the List in Step 3</li><li>Iterate through each Feed Item record in the List from Step 2. and add the random comment.</li><li>DML to insert List of newly created records</li></ol><p>&nbsp;</p><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>Defining the trigger (the trigger syntax)</h2><p>Lets name our trigger <strong>randomChatterComment. </strong>The trigger will be on the <strong>FeedItem </strong>object and will be an <strong>After Insert</strong> trigger.</p><p>But why an after insert trigger? That's because the requirement is to create a comment on the FeedItem record being inserted. And we will need the FeedItems' ID to relate the Comment to it. The ID's and other system fields are available only in an <strong>after</strong> trigger.</p><p>The code defining our trigger will be:</p><pre class="lang:default decode:true" title="Step 1">trigger randomChatterComment on FeedItem (after insert) {</pre><p></div><div
id="tab-1-step-2" class="tab_content clearfix"></p><h2>Create a List of FeedComments for the final insert</h2><p>In this step, we will create a List of FeedComments that will be used in DML (last step)</p><pre class="lang:default decode:true ">    //Create a list to hold the list of Feed Comment records being inserted
    List&lt;FeedComment&gt; List2insert = New list&lt;FeedComment&gt;();</pre><p>&nbsp;</p><p></div><div
id="tab-2-step-3" class="tab_content clearfix"></p><h2>Create a List of Strings/Comments</h2><p>In this step, we will create a list of Strings (comments) from which a random one will be selected in the next step.</p><pre class="lang:default decode:true ">//Create a list of Strings
    List&lt;String&gt; commentList = New List&lt;String&gt;{
        'Congratulations and BRAVO!',
            'This calls for celebrating! Congratulations!',
            'You did it! So proud of you!',
            'I knew it was only a matter of time. Well done!',
            'Congratulations on your well-deserved success.',
            'Heartfelt congratulations to you.',
            'Warmest congratulations on your achievement.',
            'Congratulations and best wishes for your next adventure!',
            'So pleased to see you accomplishing great things.',
            'Feeling so much joy for you today. What an impressive achievement!',
            'You’ve worked so hard for this. Congrats!',
            'This is awesome! You’re awesome! Way to go!',
            'Here’s to your streak! Keep it up!',
            'Sincere congratulations on your hard-earned success',
            'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can’t express how proud I am!',
            'You have the creativity and determination to do whatever you can dream. I hope you feel proud today and confident in your ability to rise to your next challenge.',
            'Celebrating the dedication you’ve shown on the way to this achievement. You’ve earned every bit of the success you’re enjoying.',
            'Congrats!!!'           
            };</pre><p></div><div
id="tab-3-step-4" class="tab_content clearfix"></p><h2>Get a Random Comment</h2><p>To get a random comment, we will first generate a random number. Then use the GET method to retrieve a comment from the List.</p><pre class="lang:default decode:true ">//Get a random number which is less than the size of the above list
   Integer i = (Math.random()* (commentList.size() -1)).intvalue();</pre><p></div><div
id="tab-4-step-5" class="tab_content clearfix"></p><h2>Iterate through Feed Item records and add comment</h2><p>In this step, we will iterate through each FeedItem record that entered the trigger. Remember, these are the records available in trigger.new</p><p>We will also use an If condition to check if the FeedItem record contains the word 'Success'. If yes, a we will create a FeedComment record with a random comment.</p><p>We will then add all the newly created FeedComment records to a final List that will be used in DML in the next step.</p><pre class="lang:default decode:true">    //iterate through each feed item record and add the comments
    For (FeedItem FIR:Trigger.new) { 
        //check if the post contains the word "Success"
        If (FIR.Body.contains('success')) {
            FeedComment FC = New FeedComment(); 
            FC.CommentBody = commentList.get(i);//add a random comment from the comment list using the random number generated earlier
            FC.FeedItemID = FIR.id;
            FC.CreatedById = '005f4000002NG0g'; //ID of user who will be commenting
            List2insert.add(fc); //add to final list of comments that needs to be inserted
        }
    }</pre><p>&nbsp;</p><p></div><div
id="tab-5-step-6" class="tab_content clearfix"></p><h2>DML to Insert the List of Feed Comments</h2><pre class="lang:default decode:true">Insert List2insert; // insert the final list of comments</pre><p></div></div></div><div
class="clear"></div><p>&nbsp;</p><p><strong>The complete trigger to post a random chatter comment will be as follows:</strong></p><pre class="lang:default decode:true ">trigger randomChatterComment on FeedItem (after insert) {
    
    //Create a list to hold the list of Feed Comment records being inserted
    List&lt;FeedComment&gt; List2insert = New list&lt;FeedComment&gt;();
    
    //Create a list of comments
    List&lt;String&gt; commentList = New List&lt;String&gt;{
        'Congratulations and BRAVO!',
            'This calls for celebrating! Congratulations!',
            'You did it! So proud of you!',
            'I knew it was only a matter of time. Well done!',
            'Congratulations on your well-deserved success.',
            'Heartfelt congratulations to you.',
            'Warmest congratulations on your achievement.',
            'Congratulations and best wishes for your next adventure!',
            'So pleased to see you accomplishing great things.',
            'Feeling so much joy for you today. What an impressive achievement!',
            'You’ve worked so hard for this. Congrats!',
            'This is awesome! You’re awesome! Way to go!',
            'Here’s to your streak! Keep it up!',
            'Sincere congratulations on your hard-earned success',
            'You are proof that good things come to those who are willing to sacrifice to reach a worthwhile goal. Words can’t express how proud I am!',
            'You have the creativity and determination to do whatever you can dream. I hope you feel proud today and confident in your ability to rise to your next challenge.',
            'Celebrating the dedication you’ve shown on the way to this achievement. You’ve earned every bit of the success you’re enjoying.',
            'Congrats!!!'           
            };
                //Get a random number which is less than the size of the above list
                Integer i = (Math.random()* (commentList.size() -1)).intvalue();
    
    //iterate through each feed item record and add the comments
    For (FeedItem FIR:trigger.new) { 
        //check if the post contains the word "Success"
        If (FIR.Body.contains('success')) {
            FeedComment FC = New FeedComment(); 
            FC.CommentBody = commentList.get(i);//add a random comment from the comment list using the random number generated earlier
            FC.FeedItemID = FIR.id;
            FC.CreatedById = '005f4000002NG0g'; //ID of user who will be commenting
            List2insert.add(fc); //add to final list of comments that needs to be inserted
        }
    }
    Insert List2insert; // insert the final list of comments
    
}</pre><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-post-automated-random-chatter-comment/1099/">Trigger to Post Automated Random Chatter Comment</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-post-automated-random-chatter-comment/1099/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<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>Convert Salesforce ID from 15 to 18 Characters</title><link>https://www.syedtaha.com/salesforce-admin-hacks/convert-salesforce-id-from-15-to-18-characters/1026/</link>
<comments>https://www.syedtaha.com/salesforce-admin-hacks/convert-salesforce-id-from-15-to-18-characters/1026/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Wed, 10 Oct 2018 19:13:33 +0000</pubDate>
<category><![CDATA[Salesforce Admin Hacks]]></category>
<category><![CDATA[convert salesforce id]]></category>
<category><![CDATA[salesforce id]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1026</guid><description><![CDATA[<p>Enter the 15 character Salesforce Ids in the first text box (one per line) and click on the convert button to get the 18 character Salesforce Ids.</p>
The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/convert-salesforce-id-from-15-to-18-characters/1026/">Convert Salesforce ID from 15 to 18 Characters</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1><strong>Convert the 15 character Salesforce Id&#8217;s to 18 characters.</strong></h1><p>Here is a simple tool that converts the Salesforce 15 character Ids to 18 character Ids.</p><p>This conversion of Salesforce ids from 15 character to 18 characters is done on the local machine and is safe to use.The Salesforce 15 to 18 char Id conversion tool calls Javascript from the footer of the page hence transmits no data to the server.</p><p>Enter the 15 character Salesforce Ids in the input text box (one per line) and click on the convert button to get the 18 character Salesforce Ids.</p><p>&nbsp;<br
/>
<strong>Input 15 character Salesforce Ids &#8211; one per line</strong><br
/><textarea id="inputConvertedSalesforce15to18Ids" style="height: 90%; min-height: 200px; width: 600px;"></textarea><br
/>
&nbsp;<br
/>
<button
onclick="ConvertSalesforce15to18()">Click to Convert Salesforce 15 digit Ids tp 18 digits</button><br
/>
&nbsp;<br
/>
<strong>Output: 18 character Salesforce Ids</strong><br
/><textarea id="displayConvertedSalesforce15to18Ids" style="height:90%;min-height:200px;width:600px"></textarea></p>The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/convert-salesforce-id-from-15-to-18-characters/1026/">Convert Salesforce ID from 15 to 18 Characters</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></content:encoded>
<wfw:commentRss>https://www.syedtaha.com/salesforce-admin-hacks/convert-salesforce-id-from-15-to-18-characters/1026/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item><title>Trigger to Roll Up (Summary) Contacts field to Accounts</title><link>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-roll-up-summary-contacts-field-to-accounts/1016/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-roll-up-summary-contacts-field-to-accounts/1016/#comments</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Wed, 22 Aug 2018 17:36:40 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[account]]></category>
<category><![CDATA[contact]]></category>
<category><![CDATA[delete]]></category>
<category><![CDATA[insert]]></category>
<category><![CDATA[rollup]]></category>
<category><![CDATA[rollup summary]]></category>
<category><![CDATA[trigger]]></category>
<category><![CDATA[update]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1016</guid><description><![CDATA[<p>Trigger to Roll Up (Summary) Contacts field to Accounts. The Total Salary 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. Get a List of Accounts that needs to be updated.<br
/>
1. Get a List of Contacts whose Salaries will be added<br
/>
2. Create a Map of Accounts and List of associated Contacts<br
/>
3. Iterate through each Account to update the Total Salary<br
/>
4. Iterate through each associated Contact to add the Salaries.<br
/>
5. Update the final List of Accounts using DML</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-roll-up-summary-contacts-field-to-accounts/1016/">Trigger to Roll Up (Summary) Contacts field to Accounts</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Trigger to create a Roll-up Summary of Contact&#8217;s field (salary) on Accounts</h1><h2>Requirement:</h2><ol><li>The Account object has a field named <strong>Total Salary</strong> with the API name <strong>total_salary__c</strong></li><li>The Contact object has a field named <strong>Salary</strong> with the API name salary__c</li><li>On each Account, the Total Salary field should hold the sum of Salaries from its associated Contacts. The trigger should work whenever a Contact&#8217;s Salary is updated, New Contact is Created or when an existing Contact is deleted.</li></ol><h2>Solution:</h2><p>Let&#8217;s write this <strong>Trigger</strong> in the following flow:</p><ol><li>Get a List of Accounts that needs to be updated.</li><li>Get a List of Contacts whose Salaries will be added</li><li>Create a Map of Accounts and List of associated Contacts</li><li>Iterate through each Account to update the Total Salary<ol><li>Iterate through each associated Contact to add the Salaries.</li></ol></li><li>Update the final List of Accounts using DML</li></ol><p>&nbsp;</p><p>Here&#8217;s the trigger:</p><pre class="lang:default decode:true ">trigger CustRollUpSummaryAccount on Contact (after insert,after update, after delete) {
    
    //New List to perform DML on the final List of Accounts
    List&lt;Account&gt; List2Update = New List &lt;Account&gt;();
    
    //New Set of Account Ids that'd be used to get Accounts &amp; Contacts List
    Set&lt;ID&gt; accountIds = New Set &lt;ID&gt;();
    
    //Using Context variables and populating the above Set
    If (trigger.isInsert || trigger.isUpdate) {
        for (contact c1:Trigger.New){
            accountIds.add(c1.AccountId);
        }
    } else If (trigger.isDelete) {
        for (contact c2:Trigger.Old){
            accountIds.add(c2.AccountId);
        }
    }
    
    //List of Accounts in this trigger populated using SOQL
    List &lt;Account&gt; accountList = New List &lt;Account&gt;();
    accountList = [SELECT Id FROM Account WHERE ID IN:accountIds];
    
    //List of Contacts in this trigger populated using SOQL
    List &lt;Contact&gt; contactList = New List &lt;Contact&gt;();
    contactList = [SELECT id,accountid,salary__c FROM Contact WHERE accountid in:accountIds];
    
    //New Map for Accounts and List of its Contacts
    Map&lt;Id,List&lt;Contact&gt;&gt; accConMap = New Map &lt;Id,List&lt;Contact&gt;&gt;();
    
    //Iterate through Contacts and populate the above map
    For (Contact Con:contactList) {        
        If (!accConMap.keyset().contains(con.AccountId)) {
            accConMap.put(con.AccountId, New List&lt;Contact&gt;());
        }
        accConMap.get(con.AccountId).add(con);
    }
    
    //Iterate through accounts 
    For (Account acc:accountList) {
        Double Amount = 0;    
        
        If (accConMap.get(acc.Id) != null &amp;&amp; accConMap.get(acc.Id).size() &gt; 0) {
            //Iterate through List of Contacts and add the amounts in the Salary fields
            For (Contact con1:accConMap.get(acc.Id)){
                If (con1.Salary__c != null) {
                    Amount = Amount + con1.Salary__c;
                }
            }    
            
        }
        acc.Total_Salary__c = Amount; //Update the Total Salary field on Account
        List2Update.add(acc); //add account to final update List
        
    }
    update List2Update; //update the final list of Accounts
}</pre><p>&nbsp;</p><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-roll-up-summary-contacts-field-to-accounts/1016/">Trigger to Roll Up (Summary) Contacts field to Accounts</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-roll-up-summary-contacts-field-to-accounts/1016/feed/</wfw:commentRss>
<slash:comments>3</slash:comments>
</item>
<item><title>Manually Setup a Users Password in Salesforce</title><link>https://www.syedtaha.com/salesforce-admin-hacks/manually-setup-a-users-password-in-salesforce/1001/</link>
<comments>https://www.syedtaha.com/salesforce-admin-hacks/manually-setup-a-users-password-in-salesforce/1001/#comments</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Fri, 17 Aug 2018 17:14:07 +0000</pubDate>
<category><![CDATA[Salesforce Admin Hacks]]></category>
<category><![CDATA[admin hacks]]></category>
<category><![CDATA[developer console]]></category>
<category><![CDATA[password]]></category>
<category><![CDATA[salesforce]]></category>
<category><![CDATA[setup password manually]]></category>
<category><![CDATA[system.setpassword]]></category>
<category><![CDATA[user]]></category>
<category><![CDATA[users password]]></category>
<category><![CDATA[workbench]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=1001</guid><description><![CDATA[<p>Manually Setup a Users Password in Salesforce using the Developer Console or the Workbench. Here's a step by step method to setup the users password manually. Pro tip includes creating a user interface to setup new passwords manually.</p>
The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/manually-setup-a-users-password-in-salesforce/1001/">Manually Setup a Users Password in Salesforce</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Manually Setup a User&#8217;s Password in Salesforce using Developer Console or the Workbench.</h1><p>There are times when an Administrator needs to <strong>manually setup a users password in Salesforce. </strong>Salesforce Administrator&#8217;s can <strong>reset</strong> a User&#8217;s password from the User record detail page but Salesforce user interface doesn&#8217;t allow an Administrator to setup a specific password. Here is an Awesome Admin Hack to achieved this using the Developer Console or the Workbench.</p><p>Let&#8217;s discuss each method step by step.</p><h2>Manually Setup a User&#8217;s Password in Salesforce Using the Developer Console.</h2><p>Step 1. Get the User&#8217;s Id from the URL bar on the User&#8217;s record detail page.</p><figure
id="attachment_1002" aria-describedby="caption-attachment-1002" style="width: 674px" class="wp-caption alignnone"><img
fetchpriority="high" decoding="async" class="size-full wp-image-1002" src="http://www.syedtaha.com/wp-content/uploads/2018/08/get-Salesforce-User-ID.png" alt="get Salesforce User ID" width="674" height="328" srcset="https://www.syedtaha.com/wp-content/uploads/2018/08/get-Salesforce-User-ID.png 674w, https://www.syedtaha.com/wp-content/uploads/2018/08/get-Salesforce-User-ID-300x146.png 300w" sizes="(max-width: 674px) 100vw, 674px" /><figcaption
id="caption-attachment-1002" class="wp-caption-text">get Salesforce User ID</figcaption></figure><p>Step 2. Open Developer Console</p><p>Step 3. Open the Execute Anonymous Window from Debug &gt;&gt; Open Execute Anonymous Window OR using the shortcut Ctrl + E</p><p>Step 4. Enter the following Apex code. Replace the UserId and Password with the actual values we need.</p><pre class="lang:default decode:true ">System.SetPassword('UserId','Password'); //Replace the UserId and Password</pre><p>Step 5. Click Execute</p><p>We successfully manually setup the User&#8217;s password using the developer console and an email with the new security token will is emailed to the User.</p><p>&nbsp;</p><h2>Manually Setup a User&#8217;s Password in Salesforce Using Workbench.</h2><ol><li>Visit <a
href="https://workbench.developerforce.com/login.php" target="_blank" rel="noopener">Workbench</a> (<a
href="https://workbench.developerforce.com/login.php" target="_blank" rel="noopener">https://workbench.developerforce.com/login.php</a>) and Login using your Salesforce credentials.</li><li>Navigate to Utilities &gt;&gt; <a
href="https://workbench.developerforce.com/pwdMgmt.php" target="_blank" rel="noopener">Password Management</a></li><li>Enter the User&#8217;s Id and the New Password</li><li>Click on Change Password</li></ol><p>We successfully manually setup the User&#8217;s password using Workbench and an email with the new security token will is emailed to the User.</p><p>&nbsp;</p><h2>Pro Tip: Creating a User Interface to manually setup a User&#8217;s password in Salesforce.</h2><p>Step 1. Create a New Text Field on the User Object and Name it Password__c</p><p>Step 2. Create the following trigger on the User object</p><pre class="lang:default decode:true ">trigger setPassword on User (before update) {
    for (User u:Trigger.New) {
        User oldUser = Trigger.OldMap.get(u.id);
        If ((u.Password__c != null) &amp;&amp; (oldUser.Password__c != u.Password__c)) {
            System.SetPassword(u.id,u.password__c);
            //Uncomment below line to clear password field
            //u.Password__c = null;
        }    
    }
}</pre><p>Note: Use validation rules on Password__c to only accept passwords that align with your organization&#8217;s password policies.</p><p>Here&#8217;s how the trigger looks in the developer console (image)</p><figure
id="attachment_1020" aria-describedby="caption-attachment-1020" style="width: 945px" class="wp-caption alignnone"><img
decoding="async" class="wp-image-1020 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/08/manually-setup-a-users-password-in-Salesforce.png" alt="manually setup a users password in Salesforce" width="945" height="449" srcset="https://www.syedtaha.com/wp-content/uploads/2018/08/manually-setup-a-users-password-in-Salesforce.png 945w, https://www.syedtaha.com/wp-content/uploads/2018/08/manually-setup-a-users-password-in-Salesforce-300x143.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/08/manually-setup-a-users-password-in-Salesforce-768x365.png 768w" sizes="(max-width: 945px) 100vw, 945px" /><figcaption
id="caption-attachment-1020" class="wp-caption-text">Manually setup a users password in Salesforce</figcaption></figure><p>Step 3. Now visit the User&#8217;s record detail page and enter the new password in the <strong>Password</strong> field that we created and click Save.</p><p>The User&#8217;s password will be changed and an email with the new security token will be emailed to the User.</p><p>&nbsp;</p><p>Update: To send the new password to the user by email, the trigger will be:</p><pre class="lang:default decode:true ">trigger CPTsetPassword on User (before update) {
    List &lt;String&gt; toAddresses = New List &lt;String&gt;();
    for (User u:Trigger.New) {
        User oldUser = Trigger.OldMap.get(u.id);
        If ((u.Password__c != null) &amp;&amp; (oldUser.Password__c != u.Password__c)) {
            System.SetPassword(u.id,u.password__c);
            toAddresses.add(u.Email);
            //Uncomment below line to clear password field after reset
            //u.Password__c = null;   
            
            //Sending the users new password by email
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
            mail.setSubject('Salesforce Password Reset');
            mail.setPlaintextBody('Your password was reset by the Administrator and the new Password is ' + u.Password__c);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});         
        }    
    } 
}</pre><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/manually-setup-a-users-password-in-salesforce/1001/">Manually Setup a Users Password in Salesforce</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></content:encoded>
<wfw:commentRss>https://www.syedtaha.com/salesforce-admin-hacks/manually-setup-a-users-password-in-salesforce/1001/feed/</wfw:commentRss>
<slash:comments>2</slash:comments>
</item>
<item><title>Changing Default Event Reminders in Users Settings For Multiple Users</title><link>https://www.syedtaha.com/salesforce-admin-hacks/changing-default-event-reminders-in-users-settings-for-multiple-users/983/</link>
<comments>https://www.syedtaha.com/salesforce-admin-hacks/changing-default-event-reminders-in-users-settings-for-multiple-users/983/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Thu, 16 Aug 2018 15:01:49 +0000</pubDate>
<category><![CDATA[Salesforce Admin Hacks]]></category>
<category><![CDATA[admin hacks]]></category>
<category><![CDATA[apex code]]></category>
<category><![CDATA[Set default event reminders to]]></category>
<category><![CDATA[user settings]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=983</guid><description><![CDATA[<p>Updating the &#8220;Set default event reminders to&#8221; or any other User Setting for multiple Users. &#160; Scenario: While creating an Event or Task in Salesforce, the “Reminder” Checkbox is checked by default for all users. The reminders have piled up and making it difficult to keep track of the important events and tasks. The Sales [&#8230;]</p>
The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/changing-default-event-reminders-in-users-settings-for-multiple-users/983/">Changing Default Event Reminders in Users Settings For Multiple Users</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Updating the &#8220;Set default event reminders to&#8221; or any other User Setting for multiple Users.</h1><p>&nbsp;</p><h2>Scenario:</h2><p>While creating an Event or Task in Salesforce, the “Reminder” Checkbox is checked by default for all users. The reminders have piled up and making it difficult to keep track of the important events and tasks. The Sales team wants you to uncheck the “Set default event reminders to” Checkbox for all users.</p><p>This setting is found in:<br
/>
My Settings &gt;&gt; Calendar and Reminders &gt;&gt; Activity Reminders &gt;&gt; ‘Set default event reminders to’</p><figure
id="attachment_984" aria-describedby="caption-attachment-984" style="width: 455px" class="wp-caption alignnone"><img
decoding="async" class="wp-image-984 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminders-Salesforce.png" alt="Activity Reminders Salesforce" width="455" height="381" srcset="https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminders-Salesforce.png 455w, https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminders-Salesforce-300x251.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminders-Salesforce-70x60.png 70w" sizes="(max-width: 455px) 100vw, 455px" /><figcaption
id="caption-attachment-984" class="wp-caption-text">Activity Reminders Salesforce</figcaption></figure><figure
id="attachment_985" aria-describedby="caption-attachment-985" style="width: 866px" class="wp-caption alignnone"><img
loading="lazy" decoding="async" class="wp-image-985 size-full" src="http://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminder-Settings-Salesforce.png" alt="Activity Reminder Settings Salesforce" width="866" height="601" srcset="https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminder-Settings-Salesforce.png 866w, https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminder-Settings-Salesforce-300x208.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/08/Activity-Reminder-Settings-Salesforce-768x533.png 768w" sizes="auto, (max-width: 866px) 100vw, 866px" /><figcaption
id="caption-attachment-985" class="wp-caption-text">Activity Reminder Settings Salesforce</figcaption></figure><p>&nbsp;</p><h2>Solution:</h2><p>This can be achieved by updating the User Object using Apex Code or the Data Loader</p><h2><span
style="text-decoration: underline;">Executing Apex Code Method</span></h2><p>A simple Apex code snipped can help us in unchecking “Set default event reminders to” Checkbox for multiple users. We will be using the Developer Console’s Execute Anonymous Window to run the Apex code.</p><p>To “Set default event reminders to” Checkbox to Unchecked, our Apex code will perform the following steps:</p><ol><li>Step 1: Create a List to hold Users that need an update to their settings.</li><li>Step 2: Find all Active Users whose ‘Activity Reminder Checkbox’ is ‘Checked’ and add them to the above list.</li><li>Step 3: Iterate through each user in the above list and Uncheck the Activity Reminder Checkbox</li><li>Step 4: Create a final list to update to reduce the DML statements</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"></p><p>We will create a List of Users</p><pre class="lang:default decode:true">// Create a list of Users whose setting needs to be changed
List &lt;User&gt; userList = New List &lt;User&gt;();</pre><p>&nbsp;</p><p></div><div
id="tab-1-step-2" class="tab_content clearfix"></p><p>We then write an SOQL to fetch all the users whose settings have to be changed.</p><pre class="lang:default decode:true ">//SOQL to add all users to the above list
userList = [
    SELECT Id, UserPreferencesEventRemindersCheckboxDefault
    FROM User
    WHERE UserPreferencesEventRemindersCheckboxDefault = True AND ISACTIVE = True];</pre><p>&nbsp;</p><p></div><div
id="tab-2-step-3" class="tab_content clearfix"></p><p>In this step, we will iterate through each user and change the required settings. Refer to the developer guide to know more about the settings. We will also add the users to the final list to be updated.</p><pre class="lang:default decode:true ">// Iterate through each user and update the setting
For (User u:userList) {
    u.UserPreferencesEventRemindersCheckboxDefault = False;
    userList2update.add(u);  //Add user to the final list to update
}</pre><p>&nbsp;</p><p></div><div
id="tab-3-step-4" class="tab_content clearfix"></p><p>As a best practice to reduce the DML statements, we will add the User to another list named ‘userList2Update’. Then, update all the Users in this list together using DML.</p><pre class="lang:default decode:true ">//Create a final list of Users to perform DML
List &lt;User&gt; userList2Update = New List &lt;User&gt;();


update userList2Update;  //Update the final List</pre><p>&nbsp;</p><p></div></div></div><div
class="clear"></div><p>Here&#8217;s the complete code to uncheck the &#8220;Set Default Reminders to&#8221; or any other user setting in Salesforce.</p><pre class="lang:default decode:true">//Create a final list of Users to perform DML
List &lt;User&gt; userList2Update = New List &lt;User&gt;();
// Create a list of Users whose setting needs to be changed
List &lt;User&gt; userList = New List &lt;User&gt;();

//SOQL to add all users to the above list
userList = [
    SELECT Id, UserPreferencesEventRemindersCheckboxDefault
    FROM User
    WHERE UserPreferencesEventRemindersCheckboxDefault = True AND ISACTIVE = True];

// Iterate through each user and update the setting
For (User u:userList) {
    u.UserPreferencesEventRemindersCheckboxDefault = False;
    userList2update.add(u);  //Add user to the final list to update
}

update userList2Update;  //Update the final List</pre><p>&nbsp;</p><p>Now that we have the Apex Code, lets head to the Developer Console and execute this code from the Anonymous Window (Ctrl + E)</p><p>&nbsp;</p><p>For the Data Loader method, please visit the Salesforce Help Article<br
/>
<a
href="https://help.salesforce.com/articleView?id=000004609&amp;language=en_US&amp;type=1" target="_blank" rel="noopener">https://help.salesforce.com/articleView?id=000004609&amp;language=en_US&amp;type=1</a></p><p>For a complete list of fields on the User Object, please visit the Developer Guide<br
/>
<a
href="https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm" target="_blank" rel="noopener">https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_user.htm</a></p>The post <a
href="https://www.syedtaha.com/salesforce-admin-hacks/changing-default-event-reminders-in-users-settings-for-multiple-users/983/">Changing Default Event Reminders in Users Settings For Multiple Users</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></content:encoded>
<wfw:commentRss>https://www.syedtaha.com/salesforce-admin-hacks/changing-default-event-reminders-in-users-settings-for-multiple-users/983/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>
<item><title>Trigger to change the Stage when Opportunity is Created &#8211; Triggers for Beginners</title><link>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/</link>
<comments>https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/#respond</comments>
<dc:creator><![CDATA[Taha Syed]]></dc:creator>
<pubDate>Wed, 25 Jul 2018 22:02:46 +0000</pubDate>
<category><![CDATA[Apex Triggers]]></category>
<category><![CDATA[apex trigger]]></category>
<category><![CDATA[beginners triggers]]></category>
<category><![CDATA[Opportunity Trigger]]></category>
<category><![CDATA[salesforce trigger]]></category>
<category><![CDATA[Stage]]></category>
<guid
isPermaLink="false">http://www.syedtaha.com/?p=979</guid><description><![CDATA[<p>Salesforce Apex Trigger to change the Stage/default the stage to 'Prospecting' when an Opportunity is created. In other words, when an Opportunity is created by selecting any Stage, the Stage should default back to Prospecting.</p>
The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/">Trigger to change the Stage when Opportunity is Created – Triggers for Beginners</a> first appeared on <a
href="https://www.syedtaha.com">Taha Syed | Salesforce</a>.]]></description>
<content:encoded><![CDATA[<h1>Salesforce Apex Triggers for Beginners: Change the Stage when an Opportunity is created.</h1><p><strong>Requirement:</strong> Write a Salesforce Apex Trigger to change the Stage/default the stage to &#8216;Prospecting&#8217; when an Opportunity is created. In other words, when an Opportunity is created by selecting any Stage, the Stage should default back to Prospecting.</p><p>Lets start by revisiting the Salesforce Apex Trigger Syntax:</p><pre class="lang:default decode:true " title="Salesforce Apex Trigger Syntax">trigger *triggerName* on *Object* (*before/after event*) {
    for (*Object* *Variable* : Trigger.New)      
        **Trigger Actions &amp; Code Block**
}</pre><p>We will write the trigger in the following three Steps:</p><ol><li>Define the trigger</li><li>Build a &#8216;For Each&#8217; loop to iterate through each record</li><li>Update the Stage</li></ol><p>Now lets write each Step for this trigger.</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></ul><div
class="tab_container"><div
id="tab-0-step-1" class="tab_content clearfix"></p><pre class="lang:default decode:true ">//Defining the Trigger - Trigger Name, Object and Events.
trigger DefaultStageProspecting on Opportunity (before insert) {</pre><p>&nbsp;</p><p></div><div
id="tab-1-step-2" class="tab_content clearfix"></p><pre class="lang:default decode:true ">    //For each Opportunity record being updated, assign the variable 'Opp' to it.
    for ( Opportunity Opp :Trigger.New) {</pre><p>&nbsp;</p><p></div><div
id="tab-2-step-3" class="tab_content clearfix"></p><pre class="lang:default decode:true ">// Update the Stage to 'Prospecting'    
    Opp.StageName = 'Prospecting';

    }
}</pre><p>&nbsp;</p><p></div></div></div><div
class="clear"></div><p>The complete Trigger to change the Stage when Opportunity is Created would be:</p><pre class="lang:default decode:true ">//Defining the Trigger - Trigger Name, Object and Events.
trigger DefaultStageProspecting on Opportunity (before insert) {

    //For each Opportunity record being updated, assign the variable 'Opp' to it.
    for ( Opportunity Opp :Trigger.New) {
    
	// Update the Stage to 'Prospecting'    
    Opp.StageName = 'Prospecting';

    }
}</pre><p>This is how the Trigger looks in the Developer Console (Image)</p><figure
id="attachment_980" aria-describedby="caption-attachment-980" style="width: 924px" class="wp-caption alignleft"><img
loading="lazy" decoding="async" class="wp-image-980 size-full" title="Trigger to Update StageName" src="http://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-Update-StageName.png" alt="Trigger to Update StageName" width="924" height="509" srcset="https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-Update-StageName.png 924w, https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-Update-StageName-300x165.png 300w, https://www.syedtaha.com/wp-content/uploads/2018/07/Trigger-to-Update-StageName-768x423.png 768w" sizes="auto, (max-width: 924px) 100vw, 924px" /><figcaption
id="caption-attachment-980" class="wp-caption-text">Trigger to Update StageName</figcaption></figure><p>&nbsp;</p>The post <a
href="https://www.syedtaha.com/salesforce-apex-triggers/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/">Trigger to change the Stage when Opportunity is Created – 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/trigger-to-change-the-stage-when-opportunity-is-created-triggers-for-beginners/979/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
loading="lazy" 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="auto, (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
loading="lazy" 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="auto, (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
loading="lazy" 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="auto, (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>