This document provides an overview and agenda for a session on debugging Apex triggers in Salesforce. It discusses common trigger problems like cascading triggers, governor limits, and null reference errors. It also outlines tools for working with triggers and provides examples of trigger use cases. The session aims to explore these common errors and how to solve them through code examples and using the Salesforce debug logs.
2. Safe harbor
Safe harbor statement under the Private Securities Litigation Reform Act of 1995:
This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties
materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results
expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be
deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other
financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any
statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services.
The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new
functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our
operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any
litigation, risks associated with completed and any possible mergers and acquisitions, the immature market in which we operate, our
relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of
our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to
larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is
included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent
fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor
Information section of our Web site.
Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently
available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions
based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these
forward-looking statements.
3. Apex Trigger Debugging
• Apex Triggers are a powerful feature of the Salesforce platfrom. They allow for
tremendous flexibility in building custom applications on the platform. Because they
execute within the database layer of the platform they can be used for multiple use
cases across an application.
• This flexibility can be a double edged sword. When you build triggers you are
exposing additional risk for errors into your application. During this session we will
be exploring some of these common errors and how you can solve for them.
4. Agenda
Apex Triggers Overview
• Technical Scope of Session
• Basics of Apex Triggers
• Tools to work with Triggers
Common Trigger Problems & Resolutions
• Cascading Triggers
• Apex Governor Limits
• Null Reference Errors
• Too Many SOQL Queries
5. Technical Scope
This is a Apex Trigger Session!
• Apex Trigger Code
• Apex Classes
• Stack Traces
• Trigger Execution Context
This session will be code and stack trace heavy.
• Code not Clicks
6. Basics of Apex Triggers
Execute at the database layer via DML (Data Manipulation Language)
• Insert, Update, Upsert, Delete, Un-Delete
• Before, After
What does this mean?
• Regardless of invocation point they will fire
• Standard Pages, Visualforce Pages, API calls, etc.
7. Basics of Apex Triggers
Order of Execution
• 1. Before Trigger Execution
• 2. System Validation Rules Execution
• 3. Record Saved in Memory (Not DB) (ID Assigned)
• 4. After Trigger Execution
• 5. Assignment Rules
• 6. Auto-Response Rules
• 7. Workflow Rules (Trigger Step 1 If Field Update)
• Full Order:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_or
der_of_execution.htm
11. Example Use Cases
1. When an Opportunity is “Closed Won”, automatically generate a Invoice.
2. When an Opportunity Product is added to an Opportunity, automatically
update the Opportunity Amount.
3. When an Invoice is generated, automatically link it to the Account and
update the Account Client Lifetime Revenue.
4. When an Account is set to start invoice setup, automatically link the
mailing Contact to the Invoices.
13. Complex Problem #1 – Cascading Triggers
Definition
• A trigger updates another record, which updates another record, which eventually
consumes all the trigger resources forcing an error
15. Complex Problem #1 – Cascading Triggers
Resolution
• There is no silver bullet for this issue
• Static Variables can be used to prevent recursive or cascading logic from
happening
• Reducing the number of Apex triggers and following best practices can mitigate
this issue
• Logic can be moved to Batch Apex if it is too large or complex
16. Complex Problem #2 – Null Reference Errors
Definition
• A trigger attempts to access a instance of an object that is empty
• A trigger attempts to access a property of an object that is empty
• This is common error amongst all programming languages
• Manifests itself in Apex the same as other languages (Java, etc)
18. Complex Problem #2 – Null Reference Errors
Resolution
• Perform null checks in your Apex code
• Ensure SOQL is pulling all fields referenced in your Apex code, consider using
Apex DAO objects to encapsulate queries
• Configure Objects to have default (non-null) values
19. Complex Problem #3 – Too Many SOQL Queries
Definition
• In a given Apex Trigger context, you cannot execute more than 100 SOQL
Queries
• This is typically due to either simple for loops executing SOQL, or more complex
cascading trigger logic.
21. Complex Problem #3 – Too Many SOQL Queries
Resolution
• Remove SOQL from for loops, while loops, or recursively called methods
• For large query logic, consider moving the logic into Apex Batch instead of an
Apex trigger
• Condense Child-Parent into one Query
• Make sure that triggers are bulkified
Dry Run Notes:
Mission Slide – Apex Triggers are powerful, but…. ETC
Bouncing around too much will make it feel like your just debugging. Very Specifically this is the problem, this is why we are seeing it. “This is failing because”. Slide Deck – If you pop-out to something that helps solve the problem. Add in presentation a title slide that says Debug Logs. Then pop to power-point. Basically a bookmark. Point out they are useful because they allow you to track user behavior. Maybe worthwhile to take the two pieces of code. Static Variable in Apex Trigger. DAO class give a brief example. Shoutout to Schema Browser so they know they can get to it. Threw Validation Rule throw.