No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Adding an Apex Trigger
- A Salesforce account in a sandbox Performance, Unlimited, or Enterprise Edition organization, or an account in a Developer organization.
- The MyHelloWorld Apex class.
In this step, you create a trigger for the Book__c custom object that calls the applyDiscount method of the MyHelloWorld class that you created in the previous step.
A trigger is a piece of code that executes before or after records of a particular type are inserted, updated, or deleted from the Force.com platform database. Every trigger runs with a set of context variables that provide access to the records that caused the trigger to fire. All triggers run in bulk; that is, they process several records at once.
- From Setup, click and click the name of the object you just created, Book.
- In the triggers section, click New.
- In the trigger editor, delete the default template code
and enter this trigger definition:
The first line of code defines the trigger:
It gives the trigger a name, specifies the object on which it operates, and defines the events that cause it to fire. For example, this trigger is called HelloWorldTrigger, it operates on the Book__c object, and runs before new books are inserted into the database.
The next line in the trigger creates a list of book records named books and assigns it the contents of a trigger context variable called Trigger.new. Trigger context variables such as Trigger.new are implicitly defined in all triggers and provide access to the records that caused the trigger to fire. In this case, Trigger.new contains all the new books that are about to be inserted.
The next line in the code calls the method applyDiscount in the MyHelloWorld class. It passes in the array of new books.