5 - Bulkification in Salesforce (Most Important)

Background Understanding ---> No, DML (Data Manipulation Language) and SOQL (Salesforce Object Query Language) are different in Salesforce:

  • DML (Data Manipulation Language): Used for modifying data in the database. DML operations include insert, update, delete, upsert, merge, and undelete. DML commands alter the state of the database by creating, updating, or removing records.

  • SOQL (Salesforce Object Query Language): Used specifically for querying data from the database. SOQL retrieves data but does not modify it. Common SOQL commands include SELECT, which fetches records based on specific criteria.

What are Governer Limits

Governor limits in Salesforce are enforced restrictions that help maintain system stability by preventing any single code execution from monopolizing shared resources. Since Salesforce operates on a multi-tenant architecture, these limits are crucial to ensuring fair resource allocation across all organizations using the platform.

Key types of governor limits include:

  1. SOQL Query Limits: Restrict the number of SOQL queries you can execute in a single transaction (e.g., 100 SOQL queries in synchronous Apex).

  2. DML Limits: Restrict the number of DML statements (like insert, update, delete) you can perform (e.g., 150 DML statements in a transaction).

  3. Heap Size Limit: Limits the total memory your code can use in a transaction (e.g., 6 MB for synchronous and 12 MB for asynchronous).

  4. CPU Time Limit: Caps the amount of time (in milliseconds) that a transaction can consume on the Salesforce servers (e.g., 10 seconds for synchronous and 60 seconds for asynchronous).

  5. Batch Sizes and Collection Limits: Restrict the size of lists or the number of records a batch process can handle.

  6. API Call Limits: Restrict the number of API calls an organization can make to Salesforce within a 24-hour period.

 

Bulkification

Bulkification in Salesforce optimizes code to handle large data volumes efficiently within governor limits. Key principles include:

Bulk Triggers: Process multiple records at once in triggers by using collections and batch updates.

  1. Bulk SOQL & DML: Avoid SOQL/DML inside loops to reduce database calls, using single queries for large data sets instead.
  2. Collections: Use lists, sets, and maps for bulk data handling and retrieval.
  3. Loop Efficiency: Minimize complex logic inside loops to enhance performance.

Bulkification ensures efficient, scalable, and governor-limit-compliant code for Salesforce applications.


HINT FOR OPTIMIZATION / BULKIFICATION-->

1)  Never write SOQL Queries and DML statements in the Loops

2) To avoid heapsize exception


// Method 1: nullifying variables

// free the heap size being used by accounts list

accounts = null;


// Method 2: using SOQL for loop (it gets data in batch of 200 records one by one)

for(Account acc : [SELECT Id, Type, Name FROM Account]){

}




Comments

Popular posts from this blog

API Names in Salesforce

18 - LWC - BEST PRACTICES - For accessing HTML elements and their values in JavaScript

6 - Object Relationships (Lookup and Master-Detail and Junction Objects)