function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bhanu challengebhanu challenge 

plz clarifie this............

what is the different b/w trigger_events and trigger context varaiables..................? with 1 example...

what is best pratices of trigger?
Best Answer chosen by bhanu challenge
SandhyaSandhya (Salesforce Developers) 
Hi Bhanu,
 
The Event would be you Specifying as Argument.
trigger MyTrigger on Order(before update,after update)
 
 
Below are  types of triggers events.
before insert
before update
before delete
after insert
after update
after delete
after undelete

Before triggers are used to update or validate record values before they’re saved to the database.
After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDatefield), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.
 
Trigger Context Variables.

Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.
A trigger is Apex code that executes before or after the following types of operations:
insert
update
delete
merge
upsert
undelete
For example, you can have a trigger run before an object's records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle Bin.

All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System.Trigger class.

isExecuting
Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an execute anonymous() 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
Returns true if this trigger was fired after all records were saved.
isUndelete
Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
newMap
A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
old
Returns a list of the old versions of the sObject records. Note that this sObject list is only available in the update and delete triggers.
oldMap
A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.
size
The total number of records in a trigger invocation, both old and new.


For Trigger Best Practices.

I suggest you to refer below link which has detailed explanation.
http://www.iterativelogic.com/salesforce-apex-trigger-best-practices/
 
Trigger simpleTrigger on Account (after insert) {
    for (Account a : Trigger.new) {
        // Iterate over each sObject
    }

    // This single query finds every contact that is associated with any of the
    // triggering accounts. Note that although Trigger.new is a collection of  
    // records, when used as a bind variable in a SOQL query, Apex automatically
    // transforms the list of records into a list of corresponding Ids.
    Contact[] cons = [SELECT LastName FROM Contact
                      WHERE AccountId IN :Trigger.new];
}



Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya


 

All Answers

Keshab AcharyaKeshab Acharya
Have  a look.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Thanks,
Keshab
SandhyaSandhya (Salesforce Developers) 
Hi Bhanu,
 
The Event would be you Specifying as Argument.
trigger MyTrigger on Order(before update,after update)
 
 
Below are  types of triggers events.
before insert
before update
before delete
after insert
after update
after delete
after undelete

Before triggers are used to update or validate record values before they’re saved to the database.
After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDatefield), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.
 
Trigger Context Variables.

Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.
A trigger is Apex code that executes before or after the following types of operations:
insert
update
delete
merge
upsert
undelete
For example, you can have a trigger run before an object's records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle Bin.

All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System.Trigger class.

isExecuting
Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an execute anonymous() 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
Returns true if this trigger was fired after all records were saved.
isUndelete
Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
newMap
A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.
old
Returns a list of the old versions of the sObject records. Note that this sObject list is only available in the update and delete triggers.
oldMap
A map of IDs to the old versions of the sObject records. Note that this map is only available in the update and delete triggers.
size
The total number of records in a trigger invocation, both old and new.


For Trigger Best Practices.

I suggest you to refer below link which has detailed explanation.
http://www.iterativelogic.com/salesforce-apex-trigger-best-practices/
 
Trigger simpleTrigger on Account (after insert) {
    for (Account a : Trigger.new) {
        // Iterate over each sObject
    }

    // This single query finds every contact that is associated with any of the
    // triggering accounts. Note that although Trigger.new is a collection of  
    // records, when used as a bind variable in a SOQL query, Apex automatically
    // transforms the list of records into a list of corresponding Ids.
    Contact[] cons = [SELECT LastName FROM Contact
                      WHERE AccountId IN :Trigger.new];
}



Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya


 
This was selected as the best answer