Filed under: Administration January 30, 2010

Salesforce.com has more automation features than any system I have ever seen. However, there are a few things that can’t be done without programming. A few examples are:

  • Updating a master record with a value from a child record when the master record is a Standard Object.  Cross-Object Workflow exists for Custom Objects, but not for Standard Objects.
  • Workflow will not create records other than Task records.
  • Getting around the formula compile size limit (Four Solutions to Salesforce.com “Too Big To Execute” Formula Error).

Apex Triggers can help with these and other scenarios. Triggers are Apex code that execute when specific actions are taken on a record, such as saving or creating a record. This code can then update the current record, search and update other records and create records.

Salesforce’s documentation on Triggers is embedded in the overall Apex documentation, therefore, it sort of expects you to read everything and then pull out the parts you need. In my busy world, I just need the quickest way to the solution. I had an endless search to just answer the question, “How do I Write My First Apex Trigger?”

This blog entry attempts to put together an overview of the Trigger development process as a set of tips and related URL links. It is not a primer on the Apex programming language or actually writing a Trigger, so you will have to do further reading. But, hopefully, it will cut down on some of the churn.

This might all sound very intimidating, but once you get through your first one, it actually is fairly simple. Of course, your actual problem will define the complexity of your solution.

Please note that my background is in Java programming and SQL (Oracle & Microsoft) databases. This article starts with this background, but hopefully everyone will be able to get something out of it.

How Do I Write My First Apex Trigger

  1. A couple of Apex Trigger examples:
    1. http://wiki.developerforce.com/index.php/An_Introduction_to_Apex
      1. About half way down in the Database Integration section.
    2. http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
      1. About 2/3 of the way down in the Test Methods and Bulk Operations section.
  2. Apex Triggers (and Apex, in general) cannot be created in Production.  You must use a Sandbox or a Developer Edition Organization and then deploy to production.
  3. There is a built in Apex editor in the application for basic programming and maintenance.  However, it does have problems with some browsers.
  4. The open source Eclipse IDE is available for larger programming tasks.  The Force.com Eclipse IDE plugin is required to interface with the Salesforce.com servers.
    1. Force.com IDE – http://wiki.developerforce.com/index.php/Force.com_IDE
  5. Unlike SQL databases, SQL is not used to update or create records.  SOQL is used to query and then Apex object methods are used to perform actions.  Like SQL, SOQL can retrieve multiple records in one execution.  Unlike SQL, Apex objects are manipulated one at a time.  Therefore, you spend a lot of coding retrieving records in batch, but looping through the results to update the records.
  6. Trigger.new[] and Trigger.old[] constructs provide access to the records and their field values.
    1. Trigger Context Variables – http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm
  7. If you are accustomed to writing SQL triggers as if you were working with one record and then adjusting it for multiple records, then stop right now and read the Salesforce articles on Governor Limits and bulkifying Triggers.  The Governor Limits prevent any one Trigger from consuming too many resources.  In particular, putting SOQL queries within a “for” loop will easily exceed the limits.  The Governor Limits may seem very limiting at first glance, but it is amazing how efficient you can be when required.
    1. Governor Limits – http://wiki.developerforce.com/index.php/Governors_in_Apex_Code
    2. Bulkifying – http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices
      1. Best Practices #1, #2, and #3.
  8. SOQL does not use SQL style inner and outer join syntax.  Its approach embeds a second SELECT within the main SELECT.  Also, it only allows the Lookup and Master-Child relationships that are defined within the data model.  If you need to join records that are on different ends of the data model, you will need to run multiple queries and merge with Apex code rather than try to come up with one big SOQL statement.  It is not as bad as you might think.
    1. Joins example – http://wiki.developerforce.com/index.php/Apex_Code_Best_Practices
      1. Best Practice #2
  9. You must create a test for every Trigger.  Test methods are created using Apex classes and methods.  The test methods must cover most of the logic branches in the Trigger.  The application has a technique for measuring how much of the Trigger is tested and  you will not be able to deploy the Trigger unless minimums are met.
    1. Testing method help – http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods
  10. The test method coverage percentage is cumulative of all Apex code in the Organization.  It is possible that poor coverage overall will prevent your well written Trigger and test code from deploying.  On the other hand, low overall test coverage in your development Organization does not mean your Trigger will not deploy to production.  You can test your deployment without actually deploying it with Eclipse’s validate deployment function.
  11. If you are using a Developer Edition Organization, you will need to use Eclipse to deploy your code to production.  If you are using a Sandbox, Change Sets is an additional option.  There are also third party AppExchange apps that can be used to deploy as well.
    1. Eclipse Deployment – http://wiki.developerforce.com/index.php/Deploy_Force.com_Applications_Faster

Once you find all the information and understand a few concepts, writing an Apex Trigger is fairly simple. It also gives you insight into how Salesforce.com has created such a well performing application in a shared environment.

Tags: , ,
  1. Samuli says:


    Thanks, this is a good post. I’m trying to write my first trigger and this has helped me a lot to understand the playground where I’m standing. I have to learn everything, but this guide helps me to make it effectively.

  2. Shashvat says:


    A very well guided post saving a lot of time from getting lost in the overloaded salesforce information.

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2009-2015 Hayata Takeshita