Skip to main content

#Salesforce #Apex How can developers design bulkified Apex triggers and classes to handle large volumes of records efficiently while staying within Salesforce governor limits, and what common mistakes should beginners avoid?

2 réponses
  1. 17 oct., 04:25

    Hi @Sai KumarGood question! Bulkification is one of the most important Apex development practices, especially when handling large volumes of Salesforce records. 

     

    1. Best Practices for Bulkified Apex

    1. Avoid SOQL inside loops: Query all required records at once.
    2. Avoid DML inside loops: Use Lists to perform bulk insert, update, or delete operations.
    3. Use Collections: Use List, Set, and Map to process records efficiently.
    4. Handle Multiple Records: Always design triggers to process 200 records, not just one.
    5. Use Trigger Handlers: Keep business logic in separate Apex classes.
    6. Avoid Recursion: Prevent unnecessary repeated trigger execution.
    7. Write Test Classes: Test single records, bulk records, and error scenarios.

    2. Important Governor Limits

    • SOQL Queries: 100 per synchronous transaction.
    • DML Statements: 150 per transaction.
    • SOQL Rows: 50,000 per transaction.
    • CPU Time: 10 seconds for synchronous Apex transactions.

    3. Common Mistakes to Avoid

    • Writing SOQL or DML inside loops.
    • Assuming a trigger processes only one record.
    • Running unnecessary queries and updates.
    • Not testing with 200 records.

    4. Official Salesforce Resources

    Build an Account and Contact automation. When multiple Contacts are inserted, update their related Accounts using one SOQL query and one bulk DML operation.

    Key Point:

    Always write Apex code that works for 1 record and 200 records, not just a single record. 

     

    Hope this helps! Venkat Yadav | SFDCINDIA

0/9000