Posts

Showing posts with the label trigger

Trigger Context Variables

All triggers define implicit variables that allow developers to access runtime context. These variables are contained in the System.Trigger class Let us summarize all the Trigger context variable in form of a table: Context Variable It's Usage isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or a executeanonymous() API call. isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API. isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API. isBefore Returns true if this trigger was fired before any record was saved. isAfter...

Recursive Trigger and Ways to avoid Recursive Trigger

    What is a Recursive Trigger: You want to write a trigger that creates a new record as part of its processing logic; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on.  and thus you are into recursive trigger. This is a simple way to create this error via a trigger on an custom object called TestObject__c. trigger TestTrigger on TestObject__c (before insert) { insert new Test__c(); } When the above trigger is fired, a TestObject__c object record is created by the user, which causes the trigger to execute again for this record, this keeps repeating so and on until salesforce gives error. Ways to avoid recursion: In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. 1) Use static variables to store information that is shared within the confines of the class. All instances of the same class share a single copy of the static variables. * ...