Monday, 1 June 2015

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework


1) One Trigger Per Object

A single Apex Trigger is all you need for one particular object. If you develop multiple Triggers for a single object, you have no way of controlling the order of execution if those Triggers can run in the same contexts

2) Logic-less Triggers

If you write methods in your Triggers, those can’t be exposed for test purposes. You also can’t expose logic to be re-used anywhere else in your org.

3) Context-Specific Handler Methods

Create context-specific handler methods in Trigger handlers

4) Bulkify your Code

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time.

5) Avoid SOQL Queries or DML statements inside FOR Loops

An individual Apex request gets a maximum of 100 SOQL queries before exceeding that governor limit. So if this trigger is invoked by a batch of more than 100 Account records, the governor limit will throw a runtime exception

6) Using Collections, Streamlining Queries, and Efficient For Loops

It is important to use Apex Collections to efficiently query data and store the data in memory. A combination of using collections and streamlining SOQL queries can substantially help writing efficient Apex code and avoid governor limits

7) Querying Large Data Sets

The total number of records that can be returned by SOQL queries in a request is 50,000. If returning a large set of queries causes you to exceed your heap limit, then a SOQL query for loop must be used instead. It can process multiple batches of records through the use of internal calls to query and queryMore

8) Use @future Appropriately

It is critical to write your Apex code to efficiently handle bulk or many records at a time. This is also true for asynchronous Apex methods (those annotated with the @future keyword). The differences between synchronous and asynchronous Apex can be found

9) Avoid Hardcoding IDs

When deploying Apex code between sandbox and production environments, or installing Force.com AppExchange packages, it is essential to avoid hardcoding IDs in the Apex code. By doing so, if the record IDs change between environments, the logic can dynamically identify the proper data to operate against and not fail

Always remember below points before writing trigger :-
1) Order Of Execution
2) Recursive Trigger
3) Learn about Other Trigger Framework with Recording

Here is sample code for Trigger Handler framework Code :-

Create one Trigger "AccountTrigger"

trigger AccountTrigger on Account( after insert, after update, before insert, before update)
{

    AccountTriggerHandler handler = new AccountTriggerHandler(Trigger.isExecuting, Trigger.size);
    
    if( Trigger.isInsert )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeInsert(trigger.New);
        }
        else
        {
            handler.OnAfterInsert(trigger.New);
        }
    }
    else if ( Trigger.isUpdate )
    {
        if(Trigger.isBefore)
        {
            handler.OnBeforeUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
        else
        {
            handler.OnAfterUpdate(trigger.New ,trigger.Old,Trigger.NewMap,Trigger.OldMap);
        }
    }
}

Create one Trigger Handler Class

public with sharing class AccountTriggerHandler 
{
    private boolean m_isExecuting = false;
    private integer BatchSize = 0;
    public static boolean IsFromBachJob ;
    public static boolean isFromUploadAPI=false;
    
    public AccountTriggerHandler(boolean isExecuting, integer size)
    {
        m_isExecuting = isExecuting;
        BatchSize = size;
    }
            

    public void OnBeforeInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On Before Insert');
    }
    public void OnAfterInsert(List<Account> newAccount)
    {
        system.debug('Account Trigger On After Insert');
    }
    public void OnAfterUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On After Update ');
        AccountActions.updateContact (newAccount);
    }
    public void OnBeforeUpdate( List<Account> newAccount, List<Account> oldAccount, Map<ID, Account> newAccountMap , Map<ID, Account> oldAccountMap )
    {
        system.debug('Account Trigger On Before Update ');
    }

    @future 
    public static void OnAfterUpdateAsync(Set<ID> newAccountIDs)
    {

    }      
    public boolean IsTriggerContext
    {
        get{ return m_isExecuting;}
    }
    
    public boolean IsVisualforcePageContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsWebServiceContext
    {
        get{ return !IsTriggerContext;}
    }
    
    public boolean IsExecuteAnonymousContext
    {
        get{ return !IsTriggerContext;}
    }
} 

Create one Trigger Action Class



public without sharing class AccountActions 
{
    public static void updateContact ( List<Account> newAccount)
    {
        // Add your logic here
    }
}

Source of link :-

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
https://developer.salesforce.com/page/Apex_Code_Best_Practices
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html
http://amitsalesforce.blogspot.in/2015/01/triggers-and-order-of-execution.html


Thanks,
Amit Chaudhary


24 comments:

  1. I just like your content. It will neat and clear.. Thanks for sharing.

    ReplyDelete
  2. Nice explanation Amit. Thanks for all your efforts man. :)

    Mehdi

    ReplyDelete
  3. Nice content Amit! How you would use the IsFromBachJob and IsFromAPI variables ?

    ReplyDelete
  4. What is the purspose of isExecuting and size in trigger handler?

    ReplyDelete
  5. What is the purspose of isExecuting and size in trigger handler?

    ReplyDelete
  6. Hi, it would be great to see this refactored to use the switch statement to control instead of the nested if/else statements.

    System.TriggerOperation triggerEvent = trigger.operationType;

    switch on triggerEvent {
    when AFTER_DELETE {
    }

    when AFTER_INSERT {
    }

    when AFTER_UNDELETE {
    }

    when AFTER_UPDATE{
    }

    when BEFORE_DELETE{
    }

    when BEFORE_INSERT {
    setLeadNames();
    }

    when BEFORE_UPDATE {
    setLeadNames();
    }

    when else {
    }
    }

    ReplyDelete
  7. Amit, first of all thank you for this post. I do have a question:

    What is the motivation for making the Handler "with sharing" but the Action "without sharing?" The trigger nominally acts using system privileges, so not sure why anything in the chain would be "with sharing." Could the classes "inherit sharing" so they get sharing from the calling context?

    ReplyDelete
  8. The Links mentioned above are not working makesure that it is working

    ReplyDelete
  9. Create context-specific handler methods in Trigger handlers
    Can you give simple example what meant by Create context-specific handler methods

    ReplyDelete
  10. Good morning sir

    ReplyDelete
  11. 7269281788 free fire I'd 200 damnd

    ReplyDelete
  12. कार्ड पासवर्ड

    ReplyDelete