Wednesday 25 May 2011

Writing Apex Trigger : Issues and Solutions ( Cyclic Trigger )

Hi All,

Some times we have a situation where trigger on a event of one object fires another trigger and logic in that trigger again fires first trigger. These situations are called cyclic triggers.
Ex :


trigger Trigger1 on Account (after update) 
    {
        
        for(Account acc : Trigger.New)
            {   
                //Trigger Action Logic
                List<Contact> listCon = [Select Fax from Contact where AccountId =: acc.id];      
                for(Contact con : listCon)
                    {
                        con.Fax = acc.Fax;
                    }
                update listCon;    
           
            }
    }
Another trigger on Contact object update is written like :


trigger Trigger2 on Contact (after update) 
    {
        
        for(Contact con  : Trigger.New)
            {   
                //Trigger Action Logic
                List<Account> accList = [Select Phone from Account where id =: con.AccountId];      
                for(Account acc : accList)
                    {
                        acc.Phone = con.Phone;
                    }
                update accList;    
           
            }
    }

In Above example event in Trigger1 invokes Trigger2 and event in Trigger2 again invokes Trigger1.

Solution : If we use entry criteria as explained in earlier post then cyclic trigger problem will be solved.
Writing Apex Trigger : Issues and Solutions ( Entry Criteria)


4 comments:

  1. That is the same as B2C. A huge contrast, however, is that B2B promoting can http://www.bestessaywriting.org/essay_writing.php include composing promotion duplicate, audio/visual presentations, white papers, updates, and suggestions, to name only a couple of, all administered at one client.

    ReplyDelete
  2. Not sure how B2C is same??

    ReplyDelete

Tweet