Apex Trigger Debugging
Solving the Hard Problems
Cory Cowgill, The Warranty Group
@Cory Cowgill
Safe harbor
Safe harbor statement under the Private Securities Litigation Reform Act of 1995:
This presentation may contai...
Apex Trigger Debugging
• Apex Triggers are a powerful feature of the Salesforce platfrom. They allow for
tremendous flexib...
Agenda
 Apex Triggers Overview
• Technical Scope of Session
• Basics of Apex Triggers
• Tools to work with Triggers

 Co...
Technical Scope
 This is a Apex Trigger Session!
• Apex Trigger Code
• Apex Classes
• Stack Traces
• Trigger Execution Co...
Basics of Apex Triggers
 Execute at the database layer via DML (Data Manipulation Language)
• Insert, Update, Upsert, Del...
Basics of Apex Triggers
 Order of Execution
• 1. Before Trigger Execution
• 2. System Validation Rules Execution
• 3. Rec...
Trigger Tools
 Force.com IDE
• Anonymous Apex
• Result Logs
• Unit Tests
Trigger Tools
 Salesforce Platform Debug Logs
• Stack Traces
• Per User Debugging
Apex Governor Limits
Example Use Cases
1. When an Opportunity is “Closed Won”, automatically generate a Invoice.
2. When an Opportunity Product...
Understand Your Data Model – Schema Builder
Complex Problem #1 – Cascading Triggers
 Definition
• A trigger updates another record, which updates another record, whi...
Complex Problem #1 – Cascading Triggers
Complex Problem #1 – Cascading Triggers
 Resolution
• There is no silver bullet for this issue
• Static Variables can be ...
Complex Problem #2 – Null Reference Errors
 Definition
• A trigger attempts to access a instance of an object that is emp...
Complex Problem #2 – Null Reference Errors
Complex Problem #2 – Null Reference Errors
 Resolution
• Perform null checks in your Apex code
• Ensure SOQL is pulling a...
Complex Problem #3 – Too Many SOQL Queries
 Definition
• In a given Apex Trigger context, you cannot execute more than 10...
Complex Problem #3 – Too Many SOQL Queries
Complex Problem #3 – Too Many SOQL Queries
 Resolution
• Remove SOQL from for loops, while loops, or recursively called m...
Slide parts
Slide parts
Slide parts
Slide parts
Apex Trigger Debugging: Solving the Hard Problems
Upcoming SlideShare
Loading in …5
×

Apex Trigger Debugging: Solving the Hard Problems

1,734 views

Published on

Apex Triggers can be your best friend or your worst enemy. When a trigger is firing properly your data is under control and remains sane, but when a trigger doesn't fire properly, your users can be faced with the frustration of exceptions when saving a record, or worse: incorrect data. Join us to learn tips and tricks on how to debug and solve the most complex issues, including: Ambiguous Field Validation, After Insert Activity Errors, and SOQL and Governor Limit Errors. You'll learn the origins of these kinds of advanced trigger issues and gain solutions for avoiding them.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,734
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
51
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • 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.
  • Apex Trigger Debugging: Solving the Hard Problems

    1. 1. Apex Trigger Debugging Solving the Hard Problems Cory Cowgill, The Warranty Group @Cory Cowgill
    2. 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. 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. 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. 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. 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. 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
    8. 8. Trigger Tools  Force.com IDE • Anonymous Apex • Result Logs • Unit Tests
    9. 9. Trigger Tools  Salesforce Platform Debug Logs • Stack Traces • Per User Debugging
    10. 10. Apex Governor Limits
    11. 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.
    12. 12. Understand Your Data Model – Schema Builder
    13. 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
    14. 14. Complex Problem #1 – Cascading Triggers
    15. 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. 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)
    17. 17. Complex Problem #2 – Null Reference Errors
    18. 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. 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.
    20. 20. Complex Problem #3 – Too Many SOQL Queries
    21. 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
    22. 22. Slide parts
    23. 23. Slide parts
    24. 24. Slide parts
    25. 25. Slide parts

    ×