Home Education Review on Triggers in Salesforce or Apex

Review on Triggers in Salesforce or Apex

Trigger is an event or action , As a result which gets fired up on a particular time or a event.

For example: when we set an alarm then at a particular the event/ trigger fires so that we can here the alarm .

In salesforce trigger is an apex code that can be execute either before or after on these operations- Insert ,update Delete, un delete

Catalogue for this blog:

  1. Introduction
  2. Events in Triggers
  3. Types of Triggers in Salesforce
  4. Triggers Variables
  5. Triggers in Salesforce vs Workflows in Salesforce
  6. Conclusion
  7. FAQ(Frequently Asked Questions)

Introduction

Triggers in Salesforce are also  called as  Apex Triggers. These Triggers  executes before or after Data manipulation language (DML) events occur. Apex triggers helps you to perform custom actions before or after events to record in Salesforce, such as insert, update, or delete.

Similar to database systems support triggers, Apex provides trigger support for managing the records. Therefore they  are distinct and are available specifically for common and expected actions like lead conversions.

Salesforce or apex  triggers are like stored procedures which execute when a particular event occurs. A trigger executes before and after an event occurs on record.

Also see this : Trigger tutorial

Key points:

  • Apex Trigger is also a class which contains twelve static context variables.
  • Triggers in Salesforce gets perform when a DML operation occurs on an sObject record.
  • Apex triggers  will run on Server side, but not on client side.
  • Moreover,Triggers can handle Database manipulation language (DML) operations, Which can execute SOQl and can call custom Apex methods.

When do we  use salesforce Triggers:

We should use triggers to do the tasks that can’t be done by using the point-and-click tools in the Salesforce user interface. 

Syntax of a Trigger

trigger TriggerName on ObjectName (trigger_events) {

   code_block

}

For Example:

  trigger abc on Account (before insert) {

for(account a:trigger.new){

if(a.industry==’Banking’){

a.ownership=’private’;

}}}

  • Above example shows that abc is the trigger name .Trigger Name can be anything like book,apple,school.etc
  • Object name are the objects in salesforce such as account,contact,opportunity,leads etc
  • before insert is the trigger event.
  • Between two flower braces it is the rest of the code to be written to execute a trigger

Triggers in apex or Salesforce

Events in Triggers:

Triggers events

Before Insert:

  • This trigger will be  get fired when a new record is going to be saved in the database or a table.
  • In  this, trigger  New stores a new list of records which is going to be inserted.

After Insert:

  • After insert trigger will be fired after  when a new record inserted successfully in database.
  • New is going to hold a new record which is inserted into the database.
  • It  is a read only operation. We can do Data Manipulation Language(DML) operations on new records which are inserted into the database.

Update events in salesforce are:

  • Trigger New: It is going to hold the list of records which  are going to be updated.
  • Trigger Old: This will hold the set of old values which are going to be updated.

Before Update:

This Trigger is going to store the set record before updating to the database.

After Update

  • This trigger will be fired when the changes that we have made to  saved in to the database.
  • after update trigger operation can only read the data from trigger.
  • If we want or need to make any changes we need to perform Data Manipulation Language(DML) operation.

Before Delete:

Before Delete triggers are fired automatically before a delete event occurs in a table or a database

After Delete:

This triggers are fired when after a delete event occurs in a table or a database

After Undelete

  • This after undelete trigger event works only with the  records that were deleted and then recovered from the Recycle Bin through the undelete DML statement.
  • These are also called  as undeleted records.
  • Therefore,this trigger events will run only  on top-level objects.

Types of Triggers in Salesforce

  • Before Triggers
  • After Triggers
     

Before Trigger:

  • This trigger is  used to perform the logic on the same object and specifically .
  • In General,we cant use the DML operations like Insert, update, delete on these triggers. These triggers fired before the data is beigned saved into the database.

After Trigger:

After trigger is used to perform the logic on the related objects and thus, triggers are used access the fields values that are created by system  like Created By, LastModifiedBy , Record Id etc.

Triggers
Triggers

Another Type of trigger is Bulk Triggers:

  • every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. As a result,each batch  containing of 200 records.
  • All triggers in Salesforce platform  are, by default, i.e the  bulky triggers

In  Bulky Triggers you can process many  records at a time. This  triggers can handle bulk operations and single-record updates such as:

  • Importing the data
  • Building the Mass actions
  • Making Bulk API calls
  • Moreover,Recursive Apex methods and triggers that will  invoke bulk DML statements

Check This Video

What are the considerations while implementing the Triggers?

Consider The Following Before Implementing the Triggers.

  • Upsert  trigger fires at four  different events :- before(insert, update), after (insert, update)
  • Merge trigger are fired on both  the events on delete operation
  • Field history is updated after the trigger has successfully finished  in processing data.
  • Any call out should be asynchronous so that trigger should not have to wait for the response.
  • A trigger should not  have a static keyword in its code.
  • If a trigger completes successfully the changes are also committed to the database or a table  and if the trigger is  failed  the transaction is rolled back.

Database operations:

  • Insert: Insert operation is used to create new records in Database. You can create records of any type either Standard or Custom objects this Insert DML statement.
  • Update: As we know that update means making things clear adding up to the date similarly in DML Update operation is to perform updates on the existing records
  • Upsert :This Operation is used to perform an update operation and if the records  are to be updated which are not present in database, then we can create new records as also..
  • Delete: As it says delete , so that we can delete operation in DML where the record can be deleted
  • Undelete :In this we can undelete the record which has been deleted and is or  in  the Recycle bin. All the deleted records including relationships which the deleted can also be restored.

Trigger Program in Salesforce

  • Firstly,Login in to the Salesforce account and then Click on Setup
  • Next, go to customize then select the users options
  • Then, in the user Section select Triggers option
Triggers in salesforce  org
Triggers in salesforce org
  • Next,select new and then start writing the code
Program UI in Salesforce
Program UI in Salesforce

Trigger Variables

For instance,Below Tables shows you some trigger variables and its functioning

Trigger VariableFunction
isExecuting. Returns a map of a new version of sObject records Ex:map stored in the form of map<id,newRecords>)
isInsert.   Returns true if the trigger is fired due to insert operation
isUpdate.  Returns true if the trigger is  fired due to the update operation.
isBefore.  Returns true if the trigger is fired before the record saved.
newMap.  Returns a map of a new version of sObject records Ex: map stored in the form of map<id,new Records>)
isDelete.  Returns true if the trigger is fired due to delete operation
isAfter.  Returns true if the trigger is being fired after the record saved.
Old Returns a old forms of the sObject records.
NewReturns a new forms of the sObject records
Old MapReturns the old performance of the sObject records
SizeThe total number of records in a trigger bringing in  both the  old and new.
trigger variables in apex

Triggers in Salesforce vs Workflows in Salesforce

Salesforce Workflow:

  • Salesforce Workflow is an automated process that can shoot an action which is based on evaluation and rule criteria.
  • Therefore, Performing DML operations in the workflow is not possible.
  • we can obtain a workflow over an object.
  • As a result, We  cannot  create a query from the database or a table.

Salesforce Trigger:

  • It is a piece of code which is executed either before or after a record is updated or inserted.
  • In this Trigger More than 15 DML operations can be used in a single trigger.
  • As a result,More than 20 SOQLs can be used from the database in a trigger.
  • Therefore,we can access triggers across an object and related to that object.

Complete Guide to Triggers

Also See other modules in salesforce

Conclusion

Triggers can be defined for the top-level standard objects (that supports triggers) like contact or account, with some child objects or custom objects. When triggers are created, Since ,they are active by default and Salesforce fires them on the specified event automatically.

FAQ(Frequently Asked Questions)

1.How triggers work in salesforce?

A trigger is an Apex code that executes before or after data manipulation language (DML) when an events occurs. Apex triggers helps us to perform custom actions before or after events to record in Salesforce, such as insert, update, or delete

2.When to use triggers in salesforce?

We use triggers to perform operations based on specific conditions or the criteria to modify related records or restrict certain operations from happening. we can also use triggers to do anything can also do in Apex, including executing SOQL and DML or calling an custom Apex methods.

3.What is the triggers limit in salesforce?

Apex triggers  must not exceed 200 SOQL(Salesforce Object Query Language) queries per batch. If they do, our job for that object fails. In addition, if our triggers call future methods, they are also subject to a limit of 10 future calls per batch.

RELATED ARTICLES

5 Best Laptops Under 60000 Suitable To Your Profession

Today we live in the era of the internet in which laptops and mobile phones play an important role. So if you...

5 Best Inverters to Buy This Year | How to Choose the Right Inverter

We all know inverters are useful for getting backup of electricity, but fewer people know about all features and types. If you...

Top Selling Best Budget Smartphones That You Can Buy This Year

Are you planning to buy a smartphone? Or Looking for a budget smartphones with the best features?  Almost everyone is stuck with...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

5 Best Laptops Under 60000 Suitable To Your Profession

Today we live in the era of the internet in which laptops and mobile phones play an important role. So if you...

5 Best Inverters to Buy This Year | How to Choose the Right Inverter

We all know inverters are useful for getting backup of electricity, but fewer people know about all features and types. If you...

Top Selling Best Budget Smartphones That You Can Buy This Year

Are you planning to buy a smartphone? Or Looking for a budget smartphones with the best features?  Almost everyone is stuck with...

Top 10 Network Marketing Companies in India | Future of MLM Business

Everybody knows about marketing that it is an essential part of a business. It is essential to promote our product to others...

Recent Comments