Learn and Discover How to Create a Trigger in Salesforce

Today, we are going to learn how to create a trigger in Salesforce. This is a rather involved process, and it does require getting our feet wet with some programming logic. However, we must not let this intimidate us, as once we see this done once, it makes perfect sense in the future.

How to Create a Trigger in Salesforce

In traditional programming education style, we will learn this process by creating a “Hello World” project. Programmers always learn languages by creating a program first which in some way presents this phrase. This dates back to the testing of the BASIC and FORTRAN languages of yore, and it still works for demonstrating the process today just as well.

Please note that while the process of creating a custom object is partially discussed in this tutorial, you should first familiarize yourself with them in detail. Please refer to our custom object creation tutorial before continuing.

Man and laptop - how to create a custom object in salesforce

First, we shall create a custom object, with the following steps.

Step 1 – Log into your sandbox/developer organization.

Step 2 – Click (Your Name)->Setup->Create->Objects and then click “New Custom Object”.

Step 3 – Enter “Book” for the label and “Books” for the plural label.

Step 4 – Click “Save”.

Step 5 – Click “New” in the “Custom Fields & Relationships” section of the Book detail page.

Step 6 – Select “Number” as the data type and click “Next”.

Step 7 – Enter “Price” for the field label, and enter 16 in the length text box.

Step 8 – Enter 2 in the decimal places text box and click “Next”.

Step 9 – Click “next” to accept all other default values.

Step 10 – Click “Save”.

Now, before we continue, we need to be sure we are familiar with a few programming conventions. Rather than spend several hours learning them in here, take a look at this check list and be sure you understand the purpose of each of these components in programming. If not, consult C programming conventions tutorials before continuing, as Apex is based on C#, which is in turn based on C.

1 – public, private and static declarations

2 – variables

3 – integer, float, and string variable types

4 – classes and functions

5 – flow logic (switch, if, then, else, for and while)

6 – voids

7 – returns

8 – semicolon terminations

9 – parenthetical/algebraic expressions

10 – brackets for code block encapsulation

11 – line comments

If you are familiar on at least a beginner level with these things, then we will now move on to writing our first Apex class

1 – Click (Your Name)->Setup->Develop->Apex Classes, and then “New”

2 – In the class editor, ass the class definition as such:

public class MyHelloWorld {

}

This is the class name, which will encapsulate the code for the class itself, of course.

3 – Now, inside the class encapsulation, add the following code:

public static void applyDiscount(Book_c[] books) {

for (Book_c b :Books){

b.Price_c *= 0.9;

{

{

If this code’s functionality is mystifying in any way, we suggest again stopping and consulting some C and C# documentation before continuing. We may provide some tutorials that go into detail about Apex programming in the future, if enough readers request them as well.

Step 4 – Click “Save”.

Now, we are ready to add the trigger itself

Step 1 – Click (Your Name)->Setup->Create->Objects and click the object we created earlier.

Step 2 – In the triggers section, click “New”.

Step 3 – In the trigger editor, delete the default trigger code, and enter the following:

trigger HelloWorldTrigger on Boock_c (before insert) {

Book_c[] books = Trigger.new;

MyHelloWorld.applyDiscount(books);

}

The first line of this defines the trigger, gives it a name, and links it to an object, in this case, Book_c (created previously). It provides an event (before insert) for the trigger.

The next line creates a list of records named “books”, and instantiates the trigger via Trigger.new. If you are unfamiliar with instancing, please refer to general programming documentation.

Finally, the last line invokes the applyDiscount method we wrote in the class above.

For additional information please reffer to how to create a custom object in salesforce page.

Conclusion

You have successfully learned how to add a trigger in Salesforce. Deployment and testing of this class, however, requires advanced Apex knowledge that cannot be taught in this tutorial.

For further information go to Salesforce trigger best practices.

Amanda McDonald
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 McDonald on sabtwitterAmanda McDonald on sabgoogleAmanda McDonald on sabfacebook