Skip to main content
Hi'

lets asume that i have an object named objectA and fields F1, F2 and F3, now i have a requirement like when ever a record is going to be inserted or updated with value 'XXX' in F1 field, the trigger has to be fired and field F2 should be updated with 'YYY' then again trigger has to be fired and field F3 should be updated with value 'ZZZ' on (Before insert and before update)events and finally trigger has to be fired after insert,after update and a record has to be inserted/update with the name AI on another object named objectB,can someone help me how to write this trigger, 

i am aware that it is always better to have one trigger on one object,but i would like to know how to work around on this, any sample code based on above requirement will be highly appreciated.

 
1 件の回答
  1. 2015年6月26日 14:09
    Hi Realist,

    Always create on trigger one every object and create multiple method on apex class to create the sequen

    Please check below blog for sample code :-

    http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

    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

    }

    }

    Please add all method in Action Class.

    Please mark this as solution if this will help you.

    Thanks

    Amit Chaudhary
0/9000