Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/oijoiv2f/public_html/wp-content/themes/entaro/template-posts/single/inner.php on line 23

Your first Salesforce Apex Trigger from scratch!!!

Writing your first Salesforce Apex Trigger from scratch!!!

Before we dive deep into the abyss of Salesforce Trigger, we should know a little more about Triggers and why do we need a Trigger.

Triggers are set of code lines which are scheduled to run on a DML event, i. e. when a record is created, updated, deleted, merged or recovered and automate the flow of execution before or after the DML event.

But we have another tool to automate business logic on DML event and that is Workflows. As we know that there are certain scenarios, in which we are not able to use Workflow for e.g., If we want to create a record of the separate object, on an update of standard contact object then we simply cannot use workflow. With workflow we can only perform few task i.e., field update, send email, send an outbound message and create a task.

Apex Trigger Schematics

To overcome these limitations of workflow, Salesforce provided us with Trigger. The trigger is the core of Apex when it comes to automating business logic on any DML event.

There are two places where you can write Trigger:

  1. Sandbox
  2. Developer Edition

To upload Triggers on production, we need to make sure that it passes the Salesforce quality check, and should have enough code coverage to be eligible for deployment (at least 1% Trigger’s code coverage and overall production code coverage to be 75% and above).

Once we meet these criteria we can deploy our Trigger to production. There are a couple of ways to write Trigger in Salesforce:

  1. You can go to object definition page and there you can create a trigger specific to an object (this method provides pre defined text to the trigger which helps in writing the code)
  2. Setup >> Customize >> Users >> Triggers >> New
  3. Open developer console, click on new and select apex and then select Apex Trigger.

Next, we will begin with the syntax of the Apex Trigger

Next Post: 

find interview Questions on Trigger here

Interview Questions Apex Trigger

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

2 Comments

  1. […] Your first Salesforce Apex Trigger from scratch!!! […]

  2. […] Your first Salesforce Apex Trigger from scratch!!! – SalesforceNextGen. Writing your first Salesforce Apex Trigger from scratch!!! Before we dive deep into the abyss of Salesforce Trigger, we should know a little more about Triggers and why do we need a Trigger. Triggers are set of code lines which are scheduled to run on a DML event, i. e. when a record is created, updated, deleted, merged or recovered and automate the flow of execution before or after the DML event. […]

Leave a Comment

Your email address will not be published.