Writing High Performance Code In Salesforce August 11, 2022

Writing High Performance Code In Salesforce

Salesforce is a CRM (Customer Relationship Management) platform that provides the Process of creating and maintaining relationships with business customers or consumers. An integrated cross-functional focus on improving customer retention and profitability for the company.

Salesforce runs in a multitenant environment, the Apex runtime engine strictly enforces several limits to ensure that runaway scripts do not monopolize shared resources. These limits or governors, track and enforce the statistics. If a script ever exceeds a limit, the associated governor issues a runtime exception that cannot be handled.

Governor limits are applied based on the entry point of your code. For example, if you have an anonymous block call a trigger, the limits that the Apex runtime engine uses are the limits for the anonymous block, not for the trigger.

To improve performance, we need to follow the below Best Practices:

  1. One Trigger and One flow per Object: In salesforce, we use trigger/flow for automation purposes, if we write multiple trigger/flow per object we do not have any way to find an order of execution. If we have one trigger/flow per object then easy to maintain/control the flow of execution.

Example:

  1. Bulkify logic: As per bulk design pattern, Bulkified logic give better performance and consume less server resources. As a developer for Bulkify logic use collection (Set, Map, List) and Process data element in a collection.

Example:

  1. No DML, SOQL or SOSL inside loops: In Salesforce there is a governor limit that enforces a maximum number execute DML/SOQL/SOSL in single transaction. By using DML or queries outside of loops, your code will run faster and is less likely to exceed governor limits. Use SOSL over SOQL whenever possible, SOSL much faster than SOQL.

Example:

  1. Avoid Hardcoding: Hardcoding references always brake system. Suppose if we use record id or use static instance URL in code and move logic in other Instance it will break, because record id and URL for new instance will be different. To avoid hardcoding we can use Custom Setting, Custom Metadata, or Custom Label for Configuration as per our need

Example:

  1. Proper Exception Handling: Exception handling is the process of responding to unwanted or unexpected events encounters during our logic execution. If our logic does not have proper exception handling, it will be hard to resolve runtime errors and system will not work as per expectation. Also, use Apex Limits Methods to avoid hitting governor exceptions.

Example:

Yes, I Have An App

It needs security testing, load and performance testing and it may need deployment architecture review and static code analysis.

Arrange a meeting