Skip to content
  • linkedIn
  • twitter
  • About Us
  • News letter
  • Privacy Policy
  • Contact Us
sfdcgenius

sfdcGenius

Learn Practice and Execute

salesforce
  • Home
  • Blog
  • Apex
  • Toggle search form
Triggers in Salesforce after trigger before trigger

What is Apex Triggers in Salesforce ?

Posted on February 28, 2022February 28, 2022 By sfdcGenius 4 Comments on What is Apex Triggers in Salesforce ?

Apex triggers permit you to perform custom actions before or after events to data in Salesforce, which include insertions, updates, or deletions, like database systems support triggers, Apex provides trigger support for managing records.

Table of Contents

  • When to use Apex Triggers in Salesforce?
  • Apex Triggers Syntax
      • Notes:
  • What are different type of Triggers ?
    • Before Triggers :
    • After Triggers:
  • What are context variables in Triggers ?

When to use Apex Triggers in Salesforce?

  • Use triggers to perform operations based on specific conditions, to modify related data or restrict certain operations from happening.
  • Moreover , you can use apex triggers to do whatever you may do in Apex, which include executing SOQL and DML or calling custom Apex methods.
  • Triggers can perform tasks that can’t be completed through using the point-and-click tools withinside the Salesforce user interface. For example, if validating a field value or updating a field on a record, use validation rules and workflow rules instead.
  • 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.
  • You can define triggers for top-level standard objects that support triggers, such as a Contact or an Account, some standard child objects, such as a CaseComment, and custom objects.

For more details refer to official link.

Apex Triggers Syntax

The syntax of a apex trigger definition isn’t the same as a class definition syntax. A trigger definition starts with the trigger keyword. Then name of the trigger, the Salesforce object that the trigger is related with, and the conditions under which it fires. A trigger has the subsequent syntax:

trigger TriggerName on ObjectName (trigger_events) {
code_block
}

To execute a trigger before or after ,insert, update, delete, and undelete operations, specify more than one trigger events in a comma-separated list. The events you may specify are:

  • before insert
  • after insert
  • before update
  • after update
  • before delete
  • after delete
  • after undelete

For example, the following code defines a trigger for the before insert and before update events on the Account object:

trigger myAccountTrigger on Account (before insert, before update) {
// Your code here
}

Notes:

  • The code block of a trigger cannot contain the static keyword. However, triggers can only contain keywords applicable to an inner class.
  • In addition, you do not have to manually commit any database changes made by a trigger. 
  • A trigger invoked by an insert, delete, or update of a recurring event or recurring task results in a runtime error, when the trigger is called in bulk from the Lightning Platform​ API.
  • If Apex trigger completes successfully, than any database changes are automatically committed.
  • If Apex trigger does not complete successfully, than any changes made to the database are rolled back.
  • For instance suppose that you use an after-insert or after-update trigger to change ownership of leads, contacts, or opportunities. Then if you use the API to change record ownership, or if a Lightning Experience user changes a record’s owner, no email notification is sent. Then to send email notifications to a record’s new owner, set the triggerUserEmail property in DMLOptions to true.

What are different type of Triggers ?

There are two types of triggers:

  • Before triggers
  • After triggers

Before Triggers :

Before triggers are used to update or validate record values before they’re saved to the database.

After Triggers:

After Triggers  are 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, such as logging into an audit table or firing asynchronous events with a queue. Therefore, the records that fire the after trigger are read-only.

What is Data Security in Salesforce ?

What are context variables in Triggers ?

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

VariableUsage
isExecutingReturns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsertReturns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdateReturns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBeforeReturns true if this trigger was fired before any record was saved.
isAfterReturns true if this trigger was fired after all records were saved.
isUndeleteReturns 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.
newReturns 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.
newMapA 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.
oldReturns a list of the old versions of the sObject records.
This sObject list is only available in update and delete triggers.
oldMapA map of IDs to the old versions of the sObject records.
This map is only available in update and delete triggers.
sizeThe total number of records in a trigger invocation, both old and new.

Related Post

Customization, Recent Post Tags:Customization, Salesforce Customization, Triggers

Post navigation

Previous Post: What is Data Security in Salesforce ?
Next Post: How to check whether record values are changed or not in Apex Triggers

Comments (4) on “What is Apex Triggers in Salesforce ?”

  1. Ankit says:
    March 9, 2022 at 1:11 am

    Really helpful article. Thanks 🙂

    Reply
    1. sfdcGenius says:
      March 12, 2022 at 1:19 am

      Thanks Ankit

      Reply
  2. sfdcGenius says:
    September 17, 2022 at 4:21 am

    Thank you !! keep visiting for the latest updates

    Reply
  3. anonymous says:
    December 12, 2022 at 5:46 am

    Hello there, simply become aware of your blog via Google, and found that it’s truly informative. I’m going to be careful for brussels. I’ll appreciate for those who continue this in future. Numerous other folks can be benefited from your writing. Cheers!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Admin
  • Apex
  • Apex Class
  • Apex Triggers
  • Customization
  • Flow
  • Lightning Web Component
  • Ligthing
  • LWC
  • Recent Post
  • Salesforce Security

Tags

@api @track @wire Admin Apex Apex Class Apex Trigger api connectedCallback() decorators disconnectedCallback disconnectedCallback() errorCallback errorCallback() Flow How to check whether record values are changed or not in Apex Trigger how to create modal in lwc how to create popup in lwc lighting flow LightningAlert LightningConfirm LightningPrompt lightning web component lwc modal/popup in lwc Modal in lwc notification in lwc popup in lwc renderedCallback() Role Hierarchies Salesforce Salesforce Customization salesforce flow salesforce latest update salesforce layoff sfdc sfdcGenius sfdcginus track trailhead Trigger.NewMap Vs Trigger.oldMap trigger.old vs Trigger.New wire wrapper class in apex wrapper class in salesforce

Resources

  • About Us
  • Blog
  • Contact Us
  • Home
  • Newsletter
  • Privacy Policy

Social Media

  • linkedIn
  • twitter

Copyright © 2025 sfdcGenius.

Powered by PressBook WordPress theme