salesforce-apex-best-practices

Salesforce Apex Code is the Force programming language to write custom robust business logic and is executed in Salesforce environment. Similar to other available programming languages, there are key coding principles and best practices that will help you write efficient scalable code.

In this article, we’ll discuss about the below mentioned best practices to write and design Apex Code in Salesforce:

  • Process similar task in batch.
  • Avoid SOQL Queries or DML statements inside FOR Loops.
  • Avoid Hardcoding ID’s.

1. Process similar task in Batch:

It is the best practice when working with a bulk of data records to handle and process similar sets of record together at the same time with a single API request. In simple terms, a set of the same type of records, which are to undergo same process should be handled in the same API. This will result in minimized API requests, saving from governor limit, improved API performance, better bandwidth usage, shifting computing load to a better cloud environment.

When a batch of records is to be updated through API calls in Salesforce, the APEX code trigger handler will be invoked. The batch of records is then processed under the same Apex code as a bulk.

Example of Poorly written code:

poor-apex-code-salesforce

The above-written code only handles the first record in the “Trigger.new” collection because of the syntax Trigger.new[0]

Example of code to Handle Multiple Records:

apex-code-handle-multiple-records

In the above example, Trigger.new collection is within the ‘for loop’. Now 200 records can be processed properly.

2. Avoid SOQL Queries or DML Statements inside ‘FOR Loops’ :

In the above example, we’ve seen how ‘For’ loop handles multiple records in bulk. When SOQL queries and DML statements are placed inside a ‘for loop’, these are invoked for every iteration of the loop. Because of this, governor limits will be reached, so it would be best to avoid these scenarios whenever possible.

Example of Code with SOQL Query and DML Statement inside for loop:

apex-code-with-soql-query-and-dml-statement

To avoid it, best is to execute these SOQL queries, to retrieve the data in bulk, before executing the ‘for loop’.

Render all the necessary data in a list and iterate over the results. When you want to modify the data, retrieve the data into a list and invoke your DML once on that list.salesforce-integration-with-ERP

Example of Code with SOQL Query and DML Statement outside for loop:

apex-code-with-soql-query-and-dml-statement-outside-for-loop

3. Avoid Hardcode IDs:

Hardcode ID’s in the Salesforce Apex Code should be avoided when deploying Apex code between sandbox and production environments. The code should be written to fetch the record IDs dynamically so that if the record ID’s are changed between environments, the logic can dynamically identify the proper data and work without failure.

Example of Code with Hardcoded RecordType ID’s :

hardcode-salesforce-apex-code

In the above example, the ‘RecordTypeId’ for an account is compared against a hardcoded value. If the ID changes in the production, this trigger will fail. Such scenarios should be avoided.

Example of Code with Dynamic RecordType Id’s :

using-soql-query-salesforce-apex-code

In correction to the earlier example, in this code, we are executing query to fetch the ‘RecordTypes’ name and ID dynamically using SOQL query, and the record type for accounts to be processed will be checked against these ‘RecordType’ removing any scope of error due to ID mismatch in the production and sandbox environment.

Summary

The following results can be achieved by incorporating the above principles:

  • Handling multiple records at a time, in a single trigger process via Salesforce Apex Code.
  • Prevent Governor limit by avoiding SOQL queries in ‘for loop’.
  • Avoid hardcode ID’s mismatching by providing dynamically retrieved ID’s, making code flexible across environments.

Now, you can easily integrate Salesforce CRM with your back-end ERP system and automate your business process!salesforce-integration-with-ERP

You may also like:
What are the Most Popular Salesforce Add-ons?
How to Create Salesforce Process Builder
How to Create WorkFlow in Salesforce

Top 16 Salesforce AppExchange Apps