Author - StudySection Post Views - 683 views
apex

Apex Trigger Best Practices

  1. Single trigger for each object

    An object is supposed to have only one Apex Trigger. If we create multiple Triggers for a single object, we will not be able to connect the flow of all the triggers and expect it to execute synchronously. It might happen that the apex logic conflicts with the other trigger belonging to the same object.

  2. Triggers with no logic

    There should not be any logic in the Apex trigger. It is true that we can have functions there, but a developer will not be able to write test cases for those functions as those can not be exposed for test purposes. There is an option to update or insert a record to fire a trigger, but this is not considered a good practice. Additionally, these logics can not be used at other places if we wish to. As a solution, create a handler class via which you can access all the functional methods.

  3. Handler methods with defined context.

    There should be different functions in the handler class for different contexts. For example, a function named onBeforeInsert(). This function should only be called when Trigger.isInsert AND Trigger.isBefore is true.

  4. Code bulkification

    Apex code should always be written so that it can handle bulk data at a time. The main idea behind this is to have an optimized code so that the feature works efficiently. Moreover, to avoid governor limits.

  5. No SOQL Queries or DML statements inside FOR Loop

    There should never be a SOQL query or any DML operation done inside a FOR loop. For one transaction/session, an apex request can use 100 queries. If the session tries to use beyond this limit, the process will stop immediately thus throwing a run-time exception. In case the data loader is used to push records (say more than 100) for that particular object, and the SOQL query is used inside a loop, it will throw an error.

  6. Use Collections, Streamlined Queries, and Efficient For Loops

    Always use apex collections (List or Map) to hold data retrieved from the query. Using collections and organized queries will altogether contribute to efficient code and help in running the process within the apex governor limits. Do not iterate the same collection multiple times if it can be iterated once for a logic using IF-ELSE statements.

  7. Retrieving bulk data in SOQL

    Per governor limits, the number of records that can be returned by SOQL queries in one request is 50,000. If the query has to return records more than 50,000, then use “SOQL query for loop”. This helps in processing multiple batches of records. It calls “query“ and “queryMore” asynchronous functions internally.

  8. Leverage @future annotation when required

    The future method seems to be a good option when we face governor limits issues because the asynchronous function has its own independent set of higher governor limits. It is very important to use future annotated functions to handle bulk data. The reason is, it also has its own limitations at one point. We can have only 10 future annotated functions in a single transaction.

  9. Never use Hard-Coded IDs

    Whenever we deploy apex components to different salesforce organizations, it will throw an error because that Id may belong to another object in deployed org. Also, it may happen that the Id we used in the code does not even exist there. It will break the feature. Also, it is possible that while testing you may not find the issue because the Id does exist in the deployed org.

jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri