<?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>contact | Taha Syed | Salesforce</title>
<atom:link href="https://www.syedtaha.com/tag/contact/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>Wed, 22 Aug 2018 17:37:18 +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 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>
</channel>
</rss>