"Summer 21 (Su21) Ending at 5th December 2021 And Winter 22 (WI22) Starts 9th December 2021" For more information join our telegram https://t.me/salesforcekeeda

Monday, October 29, 2018

Apex Trigger Tutorial for beginner [ 6 step to learn Apex Trigger ]

Apex Trigger Tutorial For Beginner

Apex Trigger is the type of Apex Code. Apex Trigger is also know as Apex Script. if you want to be a good developer in salesforce than you have must knowledge about Apex Trigger now its time to go for cover all the topic on it

Topic we are covered

  1. What is Apex trigger 
  2. Advantages and Disadvantages of Apex trigger 
  3. Difference between Apex Trigger and Workflow Rule
  4. How to create a trigger in salesforce

What is Salesforce Trigger or Apex Trigger?

Saleforce trigger or apex trigger is basically used in the salesforce to update, delete, insert in the data of the salesforce. Trigger is working with an languge call DML (data manipulation language
Apex trigger is the apex script and trigger  is also know as apex code. There are two type of Trigger 
  1. Before Trigger
  2. After Trigger
Before apex trigger, After Apex Trigger

Before Trigger: we are using before trigger to update of validate the salesforce data and it is all occurs before save the value in database of the saleforce 

After Trigger: we are using after trigger its used to access the value of the database its used to change the value of the data and all these things are done by After Trigger in salesforce 

Basic syntax of apex trigger and it is the type of trigger pattern in salesforce
trigger triggerName on ObjectName (trigger_events) { Trigger_code_block }

All triggers are bulk triggers by default, and can process multiple records at a time. You should always plan on processing more than one record at a time.

Bulk triggers can handle both single record updates and bulk operations like:

Data import

com Bulk API calls

Mass actions, such as record owner changes and deletes

Recursive Apex methods and triggers that invoke bulk DML statements

A trigger is Apex code that executes before or after on following types of event:

  1. insert
  2. update
  3. delete
  4. merge
  5. upsert
  6. undelete
these are  the 6 salesforce trigger event on which we are working in the salesforce

This is the simple trigger Example in salesforce

sample trigger for those my friend who are beginner in sales force development it basically perform in account tab when we are make a new entry in that and the industry type is equal to the healthcare than it show a error and nothing will save in that tab 
but the industry type is other than it will save  a new record as a use of this trigger we can save all type of industry data except of healthcare  

trigger when Account(Industry) is Equal to Healthcare then it shows an error

trigger accounttrigger on Account (before insert, after insert) {
    if(trigger.isafter && trigger.isinsert){
    for(Account acc : trigger.new){
        if(acc.Industry == 'Healthcare'){
        acc.adderror('It shows an Error');

Advantage and Disadvantages of Apex Trigger 

- Can take almost any action
- Allow complex logic
- Can update multiple records at once
- Enable developers to update multiple related objects and records

- Must be correctly bulkilified
- Requires knowledge of apex to maintain and update

The long and short of triggers is that if you can do your update via workflow then you should as an administrator can maintain this going forward. Apex should be for situations where workflow cannot meet your needs and some complex apex logic is required.

Difference between Apex Trigger and Workflow Rule

Apex Triggers Work Flow Rule
Triggers would able to work across salesforce objects. Workflow Rules will be helpful in updating the same object or the master object in master-detail relationship.
You need to do coding in trigger. Workflow rule is a declarative one and doesn’t require coding.
Triggers having event handlers which can run before saving into database and after saving into database. Workflow rules work only after some actions i.e. entry criteria’s.