I thought it may be a good idea to share some basic information on how to write a Trigger in Apex within your salesforce.com instance.
But to start, why would I need a trigger?
Well essentially, a trigger is an action performed by the system, and it's called a 'Trigger' because it is invoked, or called to execute when triggered by an action in salesforce.com.
It is really like an advanced form of workflow.
For example, if I have a business rule that on the account screen, if someone enters the employee size, greater than 500, then I want to update a field on the account to 'Enterprise Customer', I can use a trigger to do this, for example, go to:
Setup -> Develop -> Apex Classes -> New
Then choose the type as Apex Trigger.
A basic portion of code:
trigger myTrigger on Account (after insert) {
Account myAccount = trigger.new[0];
if (myAccount.Employees > 500) {
myAccount.Type = 'Enterprise Customer';
update myAccount;
}// else nothing
}
So a few things to note, in the first line:
trigger - this defines that our class is a trigger, and that the returned object will be a variable called 'trigger'
myTrigger - this is the name of my trigger, we could have called it anything really
on Account - this means that the action we are undertaking will occur when a change is made to the Account object, we can specify any object here
(after insert) - there are a variety of specifiers you can place here, after insert means that this code will execute whenever a new Account is inserted into the database, and it will execute immediately after the new account has been inserted, the modifiers you can use are:
- before insert
- before update
- before delete
- after insert
- after update
- after delete
- after undelete
Now that covers off the first line of code.
The second line of code, 'Account myAccount = trigger.new[0];', this is important.
When a trigger is executed, the code is passed a list of objects, not just one object. The list is actually an array list of sObjects which are the primitive type of objects in salesforce.com.
The reason why we are passed a list of sObjects is because salesforce.com executes triggers in batch. So for example, if you were uploading data to salesforce.com, it actually writes these to the database in batches, each batch size can vary (depending on the upload settings), but will typically be up to 200 records.
So imagine that this trigger is executing after an upload of new accounts, or for example, two users insert an account at exactly the same time, in these cases you would be passed an array of sObjects in the 'trigger' object.
So whilst my code works, it is actually badly written in this example, what we always should do is find the size of our trigger (trigger.size()), and loop through each element and perform the update on each, because in my code right now, it will only reference the first element, trigger[0].
I am assigning the value trigger[0] to an Account variable.
I can then access the fields on the account, as you can see I change the value of Type. Then I update the database. The commit to the database is fired once the code completes.
This is a basic example of how to write a trigger. There are other things such as 'Govener Limit Exceptions' and 'Order of Execution Rules' which need to be considered, but I hope this gives any budding trigger coder a start in the right direction.
The great thing about triggers, is that you can make salesforce.com do absolutely anything you want. For example, maybe every time an account is entered, you want to do an outbound web services call to an address validation service (such as QAS), and then you want to do an outbound call to your accounting package, you really can perform any logic you like, this is the really neat part.