<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rohit Kumar on Medium]]></title>
        <description><![CDATA[Stories by Rohit Kumar on Medium]]></description>
        <link>https://medium.com/@rkrkrohitkkumar?source=rss-288c81802d21------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CaBL7NuALmAv4Q_C</url>
            <title>Stories by Rohit Kumar on Medium</title>
            <link>https://medium.com/@rkrkrohitkkumar?source=rss-288c81802d21------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:39:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rkrkrohitkkumar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Apex Code Best Practices]]></title>
            <link>https://medium.com/@rkrkrohitkkumar/apex-code-best-practices-655b90f705a1?source=rss-288c81802d21------2</link>
            <guid isPermaLink="false">https://medium.com/p/655b90f705a1</guid>
            <category><![CDATA[salesforce-best-practices]]></category>
            <category><![CDATA[salesforce]]></category>
            <category><![CDATA[salesforce-development]]></category>
            <dc:creator><![CDATA[Rohit Kumar]]></dc:creator>
            <pubDate>Fri, 10 Dec 2021 12:59:04 GMT</pubDate>
            <atom:updated>2021-12-10T12:59:04.538Z</atom:updated>
            <content:encoded><![CDATA[<p>While writing any Apex Code below points should be taken care of.</p><p><strong>1) Bulkify your Code</strong> :</p><p>Bulkifying the code simply means that it should not be limited to only one record.</p><p>Ex:</p><p>List&lt;Account&gt; attachmentList = [SELECT Id, Name FROM Account];</p><p>List&lt;String&gt; attachmentListId = attachmentList[0].Id;</p><p>The above code is not bulkified as it only considers one record. Whereas the below code is bulkified as it takes multiple records.</p><p>List&lt;String&gt; attachmentListId = new List&lt;String&gt;();</p><p>for(Account acc : attachmentList) {</p><p>attachmentListId.add(acc.Id);</p><p>}</p><p><strong>2) Avoid SOQL Queries or DML statements inside FOR Loops</strong> :</p><p>SOQL queries or any DML statement like Insert, Delete or Update should not be there inside a FOR Loop because these are invoked for every iteration of the Loop and because of this the Governor Limits may reach.</p><p><strong>3) Using Collections, Streamlining Queries, and Efficient For Loops</strong> :</p><p>It is very important to use Apex Collections to efficiently query data and store the data in memory.</p><p>Please go through this article for more understanding:</p><p><a href="https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm">https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm</a></p><p><strong>4) Streamlining Multiple Triggers on the Same Object</strong> :</p><p>Try to have minimum number of trigger in an object. For starters, Triggers gets triggered on their own depending upon the events, you do not have any control over which a trigger gets initiated. All the trigger in an object share the same available resource, they does not get their own governor limit.</p><p><strong>5) Querying Large Data Sets</strong> :</p><p>As we know that SOQL query can only return 50.000 record so if you are returning large data then it is preferred to use the SOQL query FOR Loop.</p><p>Ex:</p><p>//a run-time exception is thrown if the below query returns more than 50,000 records</p><p>Account[] acc = [SELECT Id, Name FROM Account];</p><p>//instead of above you can use the below query to handle large data.</p><p>Account[] acc = new Account[];</p><p>for (List&lt;Account&gt; accts : [Select Id, Name FROM Account]){</p><p>//logic</p><p>acc.add(accts);</p><p>}</p><p>update acc;</p><p><strong>6) Use of the Limits Apex Methods to Avoid Hitting Governor Limits</strong> :</p><p>Make sure to use these methods in your code.</p><p>- Limits.getLimitQueries() — Number of SOQL Queries allowed in this Apex code context</p><p>- Limits.getLimitDmlRows() — Number of records that can be queried in this Apex code context</p><p>- Limits.getLimitDmlStatements() — Number of DML statements allowed in this Apex code</p><p>- Limits.getLimitCpuTime() — Number of CPU usage time (in ms) allowed in this Apex code</p><p>- Limits.getQueries() — Number of Queries used in this Apex code so far</p><p>- Limits.getDmlRows() — Number of rows queried in this Apex code so far</p><p>- Limits.getDmlStatements() — Number of DML statements used so far</p><p>- Limits.getCpuTime() — Amount of CPU time (in ms) used so far</p><p>- Limits.getHeapSize() — Heap size</p><p><strong>7) Avoid Hardcoding IDs</strong> :</p><p>You should always avoid any Hardcoded Id because the Id you might be using is only limited to your sandbox. So, if you deploy the code to production the that piece of code will not work since the Id is not present there. Make sure to make your code as much dynamic as you can.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=655b90f705a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>