How to write a trigger in Apex

I thought it may be a good idea to share some basic information on how to write a Trigger in Apex within your 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

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 =[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 =[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
The reason why we are passed a list of sObjects is because executes triggers in batch. So for example, if you were uploading data to, 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 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.


jb said...

Thank you for explaining why the[o] is important. I've been trying to figure that out for awhile, and your explanation was clear and simple. Thanks!

Justin Davis said...

I don't see a "new" button under either Apex Triggers or Apex Classes... Am I missing something?

(sys admin profile enterprise edition)


Justin Davis

saba said...

Hey Justin,

You must be searching for these buttons in production environment. In production you are not allowed to write scripts. Use a sandbox or a developer edition account instead and then deploy your scripts to production.


saba said...

In this post there is no mention of how to handle bulk uploads through dataloader. This will deal with only the first record in case of bulk inserts. Please modify for bulk inserts:

trigger myTrigger on Account (before insert) {
for(Account myAccount :
if (myAccount.Employees > 500) {
myAccount.Type = 'Enterprise Customer';

}// else nothing

Dr_Faulk said...

Excellent. You've got the ball rolling in my mind, sir! Exactly the pinhole I needed just to get going with all this Apex lark.

Dr_Faulk said...

I've just noticed, there's a grumble if one includes the line 'update myAccount;'. I receive errors about it not being able to deal with trigger.old and

If I leave out the 'update myAccount' line, it works fine.

Orange Proton said...

hi, I would like to know, is there any trigger able to send email to "Notes & Attachment" in the custom object.

Gil Crist said...

This post is likeable, and your blog is very interesting, congratulations.Salesforce Melbourne

Bharath Gopalan said...

I am in fact grateful to the owner of this web site who has shared this great paragraph at here.
Salesforce Melbourne

Afanasy S said...

I see, that your blog needs fresh & unique articles.I know it’s hard to write articles manually everyday, but there is solution for this. Visit: Salesforce Melbourne

Post a Comment