Skip to main content

What is trigger in Salesforce?

What is apex trigger by- codewithanas


Trigger

Triggers are the set of actions that are called automatically once DML operation is performed on any object.

A Trigger is an Apex code that executes before or after inserting or modifying a record based on the condition provided. 
There are different types of triggers based on the action going to be performed. They are Before Triggers and After Triggers. Triggers allow modification of another record of the same type or different type.

Before Trigger: They are fired before any DML operation on an object. They are mainly used for validating records. 

After Trigger: They are fired after DML operation on any object. (or) used to access field values that are set by the system (such as a record's Id or LastModifiedDate field), and to affect changes in other records. The records that fire the after trigger are read-only.

DML operation supported by apex trigger:
  1. Before Insert
  2. After Insert
  3. Before Update
  4. After Update
  5. Before Delete
  6. After Delete
  7. After Undelete
Note: Triggers in salesforce must be bulkified. (means for more than one record.[ use collection])

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

Following are the context variable available in triggers.
  1. Trigger.isExecuting
  2. Trigger.isInsert
  3. Trigger.isUpdate
  4. Trigger.isDelete
  5. Trigger.isBefore
  6. Trigger.isAfter
  7. Trigger.isUndelete
  8. Trigger.new
  9. Trigger.newMap
  10. Trigger.old
  11. Trigger.oldMap
  12. Trigger.size

  • Trigger.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.
  • Trigger.isInsert: Returns true if this trigger was fired due to an insert operation.
  • Trigger.isUpdate: Returns true if this trigger was fired due to an update operation.
  • Trigger.isDelete: Returns true if this trigger was fired due to a delete operation.
  • Trigger.isBefore: Returns true if this trigger was fired before any record was saved.
  • Trigger.isAfter: Returns true if this trigger was fired after all records were saved.
  • Trigger.isUndelete: Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is after an undelete operation)
  • Trigger.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 in before triggers.
  • Trigger.newMap: A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.
  • Trigger.old: Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers.
  • Trigger.oldMap: A map of IDs to the old versions of the sObject records. This map is only available in update and delete triggers.
  • Trigger.size: The total number of records in a trigger invocation, both old and new.

Syntax of Trigger:
trigger TriggerName on ObjectName (trigger_events) {
   code_block
}

Continue...

Comments

Popular posts from this blog

Database Methods

Database Methods Apex contains the built-in Database class, which provides methods that perform DML operations and mirror the DML statement counterparts. These Database methods are static and are called on the class name. Database.insert() Database.update() Database.upsert() Database.delete() Database.undelete() Database.merge() Unlike DML statements, Database methods have an optional allOrNone parameter that allows you to specify whether the operation should partially succeed. When this parameter is set to false , if errors occur on a partial set of records, the successful records will be committed and errors will be returned for the failed records. Also, no exceptions are thrown with the partial success option. This is how you call the insert method with the allOrNone set to false . Database.insert(recordList, false); The Database methods return result objects containing success or failure information for each record. For example, insert and update...

DML Manipulation

Manipulate Records with DML Create and modify records in Salesforce by using the Data Manipulation Language, abbreviated as DML. DML provides a straightforward way to manage records by providing simple statements to insert, update, merge, delete, and restore records. Because Apex is a data-focused language and is saved on the Lightning Platform, it has direct access to your data in Salesforce. Unlike other programming languages that require additional setup to connect to data sources, with Apex DML, managing records is made easy! By calling DML statements, you can quickly perform operations on your Salesforce records. This example adds the TCS  account to Salesforce. An account sObject is created first and then passed as an argument to the insert statement, which persists the record in Salesforce. // Create the account sObject Account acct = new Account(Name='TCS', Phone='(123)456-7890', NumberOfEmployees=100); // Insert the account by using DML insert ...

Salesforce Licenses

For Developer Edition: User Licenses (A user license determines the baseline of features that the user can access.) Name Total License Salesforce 2 Salesforce Platform 3 Customer Community Login 5 XOrg Proxy User 2 Work.com Only 3 Customer Portal Manager Custom 5 Identity 10 Customer Community Plus 5 Silver Partner 2 Gold Partner 3 Customer Portal Manager Standard           5 Force.com App Subscription 2 Customer Community Plus Login 5 Partner App Subscription 2 External Identity 5 Partner Community 5 Partner Community Login 5 Customer Community 5 Force.com – Free 2 Chatter Free 5000 Chatter External 500 High Volume Customer Portal 10...