Apex Trigger Debugging: Solving the Hard Problems

26
Apex Trigger Debugging Solving the Hard Problems Cory Cowgill, The Warranty Group @Cory Cowgill

description

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.

Transcript of Apex Trigger Debugging: Solving the Hard Problems

Page 1: Apex Trigger Debugging: Solving the Hard Problems

Apex Trigger DebuggingApex Trigger DebuggingSolving the Hard Problems

Cory Cowgill, The Warranty Group

@Cory Cowgill

Page 2: Apex Trigger Debugging: Solving the Hard Problems

Safe harborSafe 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.

Page 3: Apex Trigger Debugging: Solving the Hard Problems

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.

Page 4: Apex Trigger Debugging: Solving the Hard Problems

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

Page 5: Apex Trigger Debugging: Solving the Hard Problems

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

Page 6: Apex Trigger Debugging: Solving the Hard Problems

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.

Page 7: Apex Trigger Debugging: Solving the Hard Problems

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_order

_of_execution.htm

Page 8: Apex Trigger Debugging: Solving the Hard Problems

Trigger Tools Force.com IDE

• Anonymous Apex

• Result Logs

• Unit Tests

Page 9: Apex Trigger Debugging: Solving the Hard Problems

Trigger Tools Salesforce Platform Debug Logs

• Stack Traces

• Per User Debugging

Page 10: Apex Trigger Debugging: Solving the Hard Problems

Apex Governor Limits

Page 11: Apex Trigger Debugging: Solving the Hard Problems

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.

Page 12: Apex Trigger Debugging: Solving the Hard Problems

Understand Your Data Model – Schema Builder

Page 13: Apex Trigger Debugging: Solving the Hard Problems

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

Page 14: Apex Trigger Debugging: Solving the Hard Problems

Complex Problem #1 – Cascading Triggers

Page 15: Apex Trigger Debugging: Solving the Hard Problems

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

Page 16: Apex Trigger Debugging: Solving the Hard Problems

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)

Page 17: Apex Trigger Debugging: Solving the Hard Problems

Complex Problem #2 – Null Reference Errors

Page 18: Apex Trigger Debugging: Solving the Hard Problems

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

Page 19: Apex Trigger Debugging: Solving the Hard Problems

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.

Page 20: Apex Trigger Debugging: Solving the Hard Problems

Complex Problem #3 – Too Many SOQL Queries

Page 21: Apex Trigger Debugging: Solving the Hard Problems

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

Page 22: Apex Trigger Debugging: Solving the Hard Problems
Page 23: Apex Trigger Debugging: Solving the Hard Problems

Slide parts

Page 24: Apex Trigger Debugging: Solving the Hard Problems

Slide parts

Page 25: Apex Trigger Debugging: Solving the Hard Problems

Slide parts

Page 26: Apex Trigger Debugging: Solving the Hard Problems

Slide parts