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.
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:
- 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:
- 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)
- Setup >> Customize >> Users >> Triggers >> New
- 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: Writing a Simple Apex Trigger !!
find interview Questions on Trigger here