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.

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

Unlock the full potential of your CRM data by using Hevo for integration. Within minutes, you can seamlessly connect Salesforce to your chosen destination, automating your data workflows and gaining real-time insights.

Why Integrate Salesforce with Hevo?

  • Automated Data Sync: Automatically transfer data from Salesforce to your preferred destinations without manual intervention.
  • Real-Time Data Flow: Keep your financial data up-to-date and consistent across all platforms.
  • No-Code Platform: With Hevo’s intuitive interface, you can easily set up and manage your data pipeline with no technical skills required.

Join 2000+ Happy Customers
Industry leaders such as Thoughtspot trust Hevo for its reliable data integration solutions. 

Get started with hevo for free

Introducing Apex Triggers in Salesforce

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) {
   code_block
}

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) {
   code_block
}

“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
Integrate Salesforce to Snowflake
Integrate Salesforce Marketing Cloud to Redshift
Integrate Salesforce V2 to BigQuery

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
  • 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) {
   code_block
}

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.

Conclusion

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.

For a clearer understanding of querying deleted records, consult our guide on delete SOQL query. It offers practical examples and expert tips for efficient record retrieval.

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.

Hevo Data, with its strong integration with 150+ 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.

FAQ on Types of Apex Triggers

Can we use after delete trigger in Salesforce?

Yes, Salesforce supports the use of after delete triggers

What is the use of after undelete trigger in Salesforce?

An after undelete trigger is executed after a record is restored from the Recycle Bin.

What is the difference between before and after delete trigger?

A before delete trigger is executed before the record is deleted from the database while an after delete trigger is executed after the record is deleted from the database

What is instead of delete triggers?

Salesforce does not have a concept of instead of delete triggers, as found in some SQL databases like SQL Server.

Raj Verma
Business Analyst, Hevo Data

Raj, a data analyst with a knack for storytelling, empowers businesses with actionable insights. His experience, from Research Analyst at Hevo to Senior Executive at Disney+ Hotstar, translates complex marketing data into strategies that drive growth. Raj's Master's degree in Design Engineering fuels his problem-solving approach to data analysis.