The Usage of Salesforce Trigger Examples

What is Salesforce trigger? How can I create an apex trigger? What are the main uses of Salesforce triggers and what are the best Salesforce trigger examples to learn from? In this article, you will learn all the information related to a Salesforce trigger tutorial and Salesforce trigger examples. An apex trigger is a particular action that usually gets fired on a particular event.

If you are a Salesforce specialist or developer there are several things you need to learn about Salesforce trigger examples. In salesforce a trigger apex is a code that normally executes after or before the following operations: _Insert, _Update, _delete and _undelete.

Triggers might run after records are inserted, deleted, restored and updated or before records are updated, inserted and deleted.

Salesforce Trigger examples – Classification of apex triggers

Salesforce Triggers are Classified into Two Groups:

1. Before triggers- These triggers are used to validate or updated record values before being saved to the database.

2. After Triggers- These triggers are normally used to access field-values that are set by the database

Salesforce Events in triggers

-Before-insert, before-update, before –delete
-After-insert, after-update, after-undelete

 

Syntax for creating Salesforce Triggers

If you want to create a trigger in Salesforce, login to your Salesforce developer account and use the following syntax example
Trigger < trigger_name> on Object-name ( <events>) {
/// write your all your code here***
}
Now if you look at the above syntax, you will notice that there is a specific section for coding, trigger name and object name. If you don’t want errors, please follow the syntax as is and do not edit it to your own version.  Checking in with one of the many Salesforce tutorials is also recommended.

 

The following is one of Salesforce trigger examples for demonstration on how to create triggers
Trigger (Insertbudget) on account (after-insert)

{
Budget budg = New budget ();
Budg.previuosbudg = trigger.new [0].Name;
Budg.AccountId =Trigger.new [0].ID;
Insert cont;
}

If you look at the above example of a Salesforce trigger you will notice the use of opening and closing brackets. Do not misplace or replace the brackets with other figures, your trigger will not function and will always result in an error. Salesforce does not automatically correct wrongly written codes, do not contact Salesforce help desk for support, Login to your Salesforce developer account and make corrections on all written codes.

 

Salesforce Best Trigger Practices

1. Avoid DML statements inside (“FOR”) Loops
An apex usually gets a maximum of 100 SOQL queries before it exceeds the governor limit. If a trigger is invoked by more than 100 records of accounts, the governor limit will throw a run-time.

 

2. Use one trigger for each object
If you don’t want to encounter errors, learn to use one trigger for each object . if you use two triggers for an object, you will not be able to control the order of executions if both triggers run in the same contexts.

 

3. Make sure you bulkify your apex code
After coding your triggers, verify that you have correctly written the codes and no functions handles more than one record at a particular given time.

 

4. Minimize large data sets in your triggers
The maximum number of records that can be returned by a SOQL query is 50,000. If you have large set of queries that cause you to exceed the limited heap, use SOQL query for all loops.

 

You can use these Salesforce trigger examples to create your own Salesforce triggers. Use your Salesforce developer account to code and implement Salesforce triggers.

Amanda McDonnald
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.
Amanda McDonnald on sabtwitterAmanda McDonnald on sabgoogle