Apex-Trigger’s Introduction
Apex Triggers In Salesforce
Highlights
- What are Triggers in Salesforce?
- DML Operation in salesforce
- Classification of Trigger
- Types Of Trigger Events
- Types of Trigger Context Variables
- When should we use trigger
- Conclusion
What is Trigger?
The trigger is a script or a block of code that gets fired automatically whenever DML operations are performed on the object.
What Are The DML Operation in salesforce?
· Insert
· Update
· Delete
· Undelete
· Upsert (Insert and Update)
· Merge (Update and Delete)
· emptyRecycleBin
· ConvertLead
Classification Of Trigger’s:=
a. Apex Triggers (Sync triggers)
The synchronous trigger is a process to perform the operation Sequential Form. It means the process waits for the task to be completed and then moves to the next task Sequentially.
Sync triggers are of two types:=
i. Before Triggers
These triggers are fired before the new data gets saved to the database.
a. Need to make changes in records that have initiated trigger.
b. Need to validate data
ii. After Triggers
These triggers are fired after the records are saved to the database.
a. Need to perform DMLs on records other than those who have initiated trigger
b. Need to call Async threads or need to make a call out to a third-party system.
b. Async Apex Triggers:=
These triggers get called asynchronously after data gets committed to the database. (In Asynchronous apex does not wait for the task to complete to move on to the next table)
Types Of Trigger Events:=
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions.
Just like database systems support triggers, Apex provides trigger support for managing records.
Triggers can be defined for top-level standard objects, such as Account or Contact, custom objects, and some standard child objects. Triggers are active by default when created. Salesforce automatically fires active triggers when the specified database events occur.
Trigger Syntax:-
A trigger definition starts with the trigger keyword. It is then followed by the name of the trigger, the Salesforce object that the trigger is associated with, and the conditions under which it fires. A trigger has the following syntax.
trigger TriggerName on ObjectName (trigger_events) { code_block } |
The events you can specify are:-
Types Of Trigger Context Variables in Salesforce:=
isBefore | Returns true if this trigger was fired before any record was saved. |
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. |
isAfter | Returns true if this trigger was fired after all records were saved. |
isDelete | Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API. |
isUndelete | Returns true if this trigger was fired after a record is recovered from the Recycle Bin. This recovery can occur after an undelete operation from the Salesforce user interface, Apex, or the API. |
isExecuting | Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call. |
new | Returns a list of the new versions of the sObject records. This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified before triggers. |
newMap | A map of IDs to the new versions of the sObject records. This map is only available before update, after insert, after update, and after undelete triggers. |
old | Returns a list of the old versions of the sObject records. This sObject list is only available in update and deletes triggers. |
oldMap | A map of IDs to the old versions of the sObject records. This map is only available in update and deletes triggers. |
size | The total number of records in a trigger invocation, both old and new. |
operationType | Returns an enum of type System.TriggerOperation corresponding to the current operation. Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states. |
When should we use trigger?
A)Logic or manipulation which can’t be done using declarative ways:=
— A parent having child record and we want to stop deletion of parent record it cannot be possible declarative ways.
B)Operations on non-related objects:=
—we want to update Account Records and Same time update Position Records both don’t have a relationship.
C)Near real-time integration with External Systems:=
—We want to connect Salesforce org to External Org
Conclusion:=
Apex Triggers is a powerful tool for organizations looking to automate complex processes and solve complex problems that couldn’t solve with workflow and process builder. Also, follow the best practices with trigger patter that our code is reusable, maintainable, and controlling logic better way.
You can learn more about Salesforce’s Apex triggers and find code examples Also online learning platform and you can learn more about Apex triggers in detail.