Types of Apex Triggers: Focus on Trigger After Delete Salesforce


Trigger After Delete Salesforce Blog Cover

Marc Benioff founded Salesforce.com in 1999, and today, Salesforce is one of the most dominant software on the planet. So, what exactly is Salesforce? Salesforce is a Cloud-Based “Customer Relationship Management” (CRM) software. It provides a full-fledged Customer Relationship Management solution to businesses without spending much effort on building one. Salesforce’s services allow businesses to use Cloud Technology to better connect with clients, customers, and partners.

Apart from its traditional Customer Relationship Management service, Salesforce has now started offering many more services such as Sales Cloud, Marketing Cloud, Mobile Connectivity, etc. As companies begin to explore the number of ways in which they use Salesforce, they look for the possibility of customization and automation.

Salesforce comprises two parts, first is the out-of-the-box functionality which in general terms is referred to as the “Configuration Part”. The second one is the “Customization Part”, where you look to enhance the out-of-the-box functionalities of Salesforce using custom logic. When it comes to customization, the first thing that comes to our mind is Apex Triggers. This article deals with an important element of the customization possibility – Salesforce Apex Triggers, with a focus on Trigger After Delete Salesforce. You’ll be taken through different types of Apex Triggers, Trigger Events, and Context Variables. You’ll also see an example of Trigger After Delete Salesforce in the latter sections. Before getting into the details, let’s discuss this robust CRM platform in brief.

Table of Contents

Introduction to Salesforce

For plenty of Entrepreneurs, Business Owners, and Corporations, Salesforce is the king of CRM. Salesforce is the most popular and robust CRM Software used by online businesses. With a name that big, Salesforce CRM doesn’t compromise its reputation.

Trigger After Delete Salesforce: Salesforce Logo
Image Source: www.en.wikipedia.org/wiki

Salesforce offers automated ways to take care of specific tasks in the system. Salesforce will automate a lot of your Marketing Efforts and will help you to accomplish your Marketing Goals. It stores all the information of your customers and Marketing Leads, and lets you keep track of Customer Data and Buying Patterns. You can also generate Sales Forecast Reports with Salesforce to convert your Leads. Salesforce also supports Email Integration with applications like Microsoft Outlook, Gmail, etc. They really have just about everything that you could possibly think of when it comes to operations in a business and managing their customers.

Salesforce follows a subscription-based model and offers a variety of pricing options, ranging from $25 to about $300 per user monthly.

You can possibly think of combining your Salesforce data with data coming from various other sources, in your Data Warehouse to get valuable insights for your business. You can use Automated Tools like Hevo Data to integrate data between Salesforce and other sources, to a Data Warehouse of your choice.

Check out our Ultimate Guide on Salesforce.

Simplify Salesforce ETL and Data Integration using Hevo’s No-code Data Pipeline

Hevo Data is a No-code Data Pipeline that offers a fully managed solution to set up data integration from Salesforce (one of the 30+ Free Data Sources) and 100+ Data Sources and will let you directly load data to a Data Warehouse or the destination of your choice. It will automate your data flow in minutes without writing any line of code. Its fault-tolerant architecture makes sure that your data is secure and consistent. Hevo provides you with a truly efficient and fully automated solution to manage data in real-time and always have analysis-ready data.

Get started with hevo for free

Let’s look at some of the salient features of Hevo:

  • Fully Managed: It requires no management and maintenance as Hevo is a fully automated platform.
  • Data Transformation: It provides a simple interface to perfect, modify, and enrich the data you want to transfer.
  • Real-Time: Hevo offers real-time data migration. So, your data is always ready for analysis.
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Scalable Infrastructure: Hevo has in-built integrations for 100’s of sources that can help you scale your data infrastructure as required.
  • Live Monitoring: Advanced monitoring gives you a one-stop view to watch all the activities that occur within Data Pipelines.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-day free trial!

Introducing Apex Triggers in Salesforce

Trigger After Delete Salesforce: Triggers
Image Source: www.techilaservices.com/blog

So, what are Apex Triggers and what importance do they hold when it comes to customization?

A Salesforce Trigger is nothing but a piece of code that can perform custom actions, executable before or after Data Manipulation Language (DML) events to record in Salesforce. These events can include activities such as inserting new records into a database, deleting records from a database, or updating records within your Salesforce. Apex Triggers within Salesforce are developed to help you automate specific tasks in the system.

Let’s consider a real-life example. Suppose, you’re going to watch a movie in a Cinema Theater. There is a Security Guard over there who is making checks. This Security Guard has custom logic in his mind, he will check whether you have a valid ticket or not. If you have a valid ticket, then the Security Guard will allow you to enter the theatre, otherwise, you will be expelled from the theater. You can think of this Security Guard as an Apex Trigger and consider yourself as an object trying to enter the Salesforce Database, which is the Cinema Theater here. So, your Apex Trigger contains custom logic and performs actions based on it. Makes sense, right?

When to Use Apex Triggers?

You can make use of triggers whenever you want to achieve functionality based on the events occurring on an object. In Salesforce, you already have pre-built applications that provide powerful CRM functionality. The first one is Workflow Routes, and then we also have Process Builders. Depending on the complexity of your business processes, if these declarative applications are not able to fulfill your requirements, you can always explore Apex Triggers.

Use triggers to perform tasks that can’t be performed by the point-and-click tools of Salesforce.

Types of Triggers

There are two different types of Apex Triggers within Salesforce:

  • “Before” Triggers: Before Triggers are used to update or validate record values before saving them to your Salesforce Database. They are used to perform the custom logic on the same object.
  • “After” Triggers: After Triggers are used to access the values of a record that are stored in the database and to update other records in your Salesforce Database using that value. They are used to implement the logic on the related records/objects. After Trigger records are “read-only”.

Both types of triggers are useful for managing records within your Salesforce Database.

Trigger Syntax

Now that you have a basic understanding of Apex Triggers, let’s have a look at how a trigger is defined on a Salesforce Object.

trigger TriggerName on ObjectName (trigger_event_types) {

Let’s consider the example of the Security Guard discussed in the previous section to understand this better.

trigger SecurityGuard on Viewer (trigger_event_types) {

“Trigger” is a Keyword here, “SecurityGuard” is a Trigger Name, and “Viewer” is an Object Name. Different “Event Types” for which you want the trigger to fire are defined in the parentheses, it could be before or after DML. The custom logic will be placed in the “code_block” section.

Here is a list of Trigger Events you can define for firing Apex Triggers within the Salesforce system. You can specify multiple Trigger Events in the parentheses separated by a comma.

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

Where to Write Triggers in the Salesforce System?

Figuring out where to practice writing Apex Triggers within the Salesforce system? There are two places where you can write Apex Trigger codes in Salesforce without affecting your Salesforce Database:

  • Sandbox – Most people prefer to write their Apex Triggers in Sandbox. Sandboxes are user-friendly copies of your normal .org and can be created at any time.
Trigger After Delete Salesforce: Sandbox
Image Source: www.cloudhq.net/synchronize
  • Developer Edition – Well, if you don’t want to use Sandbox, this is an alternative option for practicing Apex Triggers.
Trigger After Delete Salesforce: Salesforce Developers
Image Source: www.developer.salesforce.com

Context Variables in Trigger

Context Variables are used to access the records that caused the trigger to fire. Let’s have a look at different Context Variables within the Salesforce system and their purpose.

New, Old, NewMap, and OldMap are useful for holding the records whenever the trigger fires.

  • New: Trigger.New holds a collection of the new version of sObject records.
  • Old: Trigger.Old holds a collection of an old version of sObject records.
  • NewMap: Trigger.NewMap also holds a collection of the new version of sObject records, but the records are in the format of a map.
  • OldMap: Trigger.OldMap holds a map of an old version of sObject records.

The next set of Context Variables will return true or false values, i.e. Boolean results.

  • isExecuting: It will check if you’re calling any class or method from the Apex Trigger.
  • isInsert: It will check if this trigger was fired due to an insert operation.
  • isUpdate: It will check if this trigger was fired due to an update operation.
  • isDelete: It will check if this trigger was fired due to a delete operation.
  • isBefore: It will check if this trigger was fired before any record was saved.
  • isAfter: It will check if this trigger was fired after all records were saved.
  • isUndelete: It will check if this trigger was fired after a record is recovered from the Recycle Bin.

By now, you must have got a good understanding of Apex Triggers. Let’s try to understand it better with an example of Trigger After Delete Salesforce.

Trigger After Delete Salesforce

Salesforce After Delete Trigger executes the custom logic after the data is deleted from the Salesforce Database. If you are looking to delete related records, you can make use of Trigger After Delete Salesforce.

Trigger After Delete Salesforce: Syntax

You can define a Trigger After Delete Salesforce on a sObject in the following way.

trigger triggerName on objectName (after delete) {

Trigger After Delete Salesforce: Example

Let’s see a small example of the implementation of Trigger After Delete Salesforce. Suppose, after an Account Record is deleted, you want to delete the Child Object Records associated with it. “Childobject” is a related list on Account.

trigger AccountMainTrigger on Account (after delete) { 
    createContactClass obj=new createContactClass(); // Creating the instance of the apex class.
    if(trigger.isafter && trigger.isdelete) // Using context variable.
     obj.method1(Trigger.old);  // Calling apex class method.

This example of Trigger After Delete Salesforce tells you how useful Apex Triggers are for customization and automation, and this is just the beginning of what Apex Triggers can do. You can similarly implement other Trigger Events for your specific tasks.


Apex Triggers can be a powerful tool for organizations looking to automate complex processes, revolutionizing data management practices. Apex Triggers look to improve Data Management Operations and Data Management Workflows. In this article, you were provided with a comprehensive guide on Apex Triggers and understood the importance they hold in Salesforce when it comes to customization. The article concluded with a peek into Trigger After Delete Salesforce.

If you are an advanced user of Salesforce, you are most probably dealing with a lot of data sources, both internally and from other Software-as-a-Service (SaaS) offerings. Having all this data in one place helps in analysis and accelerates the Decision-Making Process. Accomplishing this requires integration to all such third-party apps and services. Salesforce’s Scheduled Data Load is a recommended approach by Salesforce. But this approach can be ineffective when you’re dealing with a large number of diverse data sources. In such cases, Hevo Data can be a great asset in your ETL tool portfolio.

visit our website to explore hevo

Hevo Data, with its strong integration with 100+ Sources & BI tools such as Salesforce (Free Data Source), allows you to not only export data from sources & load data in the destinations, but also transform & enrich your data, & make it analysis-ready so that you can focus only on your key business needs and perform insightful analysis using BI tools. You can easily extract data from Salesforce using Hevo to track the performance of your business and optimize it further to increase revenue.

Give Hevo Data a try and sign up for a 14-day free trial today. Hevo offers plans & pricing for different use cases and business needs, check them out!

Do you use Salesforce? Share your experience of working with Trigger After Delete Salesforce in the comments section below.

Raj Verma
Business Analyst, Hevo Data

Raj is a skilled data analyst with a strong passion for data analysis and architecture, having a flair for writing technical content as well. With extensive experience in handling marketing data, Raj has adeptly navigated abstract business problems to derive actionable insights that drive significant results.

No-code Data Pipeline For Your Data Warehouse