All posts

Apex Triggers

Trigger:

Trigger is an action or a sequence of actions which occurs on an Event. In Salesforce there are following events on which triggers are supposed to be fired:
  • before Insert & after Insert
  • before update & after update
  • before delete & after delete
  • undelete
Writing the first trigger:

1. Go to the detail page of the object on which you want to write the trigger. In this blog we are going to write a trigger on Account.
2. Click Setup -> Customize -> Accounts -> Triggers. Click the New button.
3. Trigger Requirement: Create a copy of an account whenever an account is inserted.

trigger FirstTrigger on Account (after insert)
{
/* FirstTrigger is the name of our trigger. Account is the object name on which we are writing the trigger. after insert is the event on which our trigger will be fired. Deciding the event of the trigger according to the requirement is very crucial and important. */

For(Account acc : Trigger.new)
{
/* In this For loop, 'Account' is the datatype or sObject type of the list on which we are rotating the loop. It iterating over the Trigger.new list with an iterator named as 'acc'. Trigger.new returns the List of the records which are newly inserted or updated, depending upon the trigger event. So here Trigger.new will return the list of those accounts which are newly inserted. */

Account newacc = new Account();
/* Since our requirement is to copy the account so we are creating an instance of Account sObject. But this above statement doesn't copy the account. It is just creating the new account.*/

newacc = acc;
/* Now we have copied the account by assigning the iterator to the new instance. By doing this all the information for this particular record will be copied. But still our copied account doesn't exist. */

insert newacc;
/* Now we have successfully copied our account by inserting it. You can now copy and paste the code to test this trigger. Required code is written in italics. */
}
}

NOTE: This trigger is only for learning purpose. This is not bulkified trigger. It may throw errors in real time situations because DML statements are called in the FOR loop. We will get back to this later on.

Some Important Points to Remember:
  1. There are some limitations in salesforce known as Governor Limits. These limits should be taken care of while coding for the smooth execution of our code.
  2. Some important governor limits are:
    • No. of DML statements should not be more than 150.
    • No. of SOQL queries should not be more than 100.
    • No. of Query Rows should not be more than 50000.
    • No. of SOSL queries should not be more than 20.
    • No. of DML rows should not be more than 10000.
    • No. of code statements should not be more than 200000.
  3. DML statements, SOQL and SOSL queries should not be written in any loop.
Refined version of the above trigger:

trigger FirstTrigger on Account (after insert)
{
List<Account> newAccounts = new List<Account>(); // List of Account type

For(Account acc : Trigger.new)
{
Account newacc = new Account();
newacc = acc;
newAccounts.add(acc); //Adding the instance of accounts which are to be inserted.
}

insert newAccounts; // Inserting the List of accounts outside the for loop.
}

Using Map to safeguard the governor limits:

Maps can significantly safeguard the governor limits and give accurate results while mapping the child records to the parent. When bulk data is processed then it is very necessary to use maps in our code.

First published on Blogger in 2013. Some screenshots and APIs have changed since; the code is kept as written.

  • apex trigger
  • Salesforce
  • salesforce apex trigger
  • triggers