deep dive into apex triggers

42
Deep Dive into Apex Triggers June 18, 2015

Upload: salesforce-developers

Post on 02-Aug-2015

1.113 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Deep Dive into Apex Triggers

Deep Dive into Apex TriggersJune 18, 2015

Page 2: Deep Dive into Apex Triggers

#forcewebinar

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: Deep Dive into Apex Triggers

#forcewebinar

Speakers

Kartik ViswanadhaDirector, OpFocusSalesforce MVP@logontokartik

Page 4: Deep Dive into Apex Triggers

#forcewebinar

Go Social!

Salesforce Developers

+Salesforce Developers

Salesforce Developers

Salesforce Developers The video will be posted to YouTube & the webinar recappage (same URL as registration).

This webinar is being recorded!

@salesforcedevs / #forcewebinar

Page 5: Deep Dive into Apex Triggers

#forcewebinar

Pre-requisites

• Basic understanding of Apex Triggers and Apex Code.

• Basic understanding of Salesforce Platform in general

Page 6: Deep Dive into Apex Triggers

#forcewebinar

What is a Trigger?

Which of the below definitions is right for our context?

Page 7: Deep Dive into Apex Triggers

#forcewebinar

What is a Trigger in Database terms?

A trigger is a procedure that initiates an action (i.e., fires an action) when an event occurs.

Insert

Update

Delete

Undelete

Page 8: Deep Dive into Apex Triggers

#forcewebinar

Trigger Order of Execution

B A

Save State Commit

System Validation Rules

Apex Before Triggers

Custom Validation Rules

Apex After Triggers

Assignment Rules

Workflow Rules

Escalations

Rollup Summary Fields

efore fter

Page 9: Deep Dive into Apex Triggers

#forcewebinar

Trigger Contexts

B A

Save State Commit

• Trigger.new• Trigger.old• Trigger.newMap• Trigger.oldMap

• Trigger.isBefore• Trigger.isAfter• Trigger.isExecuting• Trigger.size

• Trigger.isInsert• Trigger.isUpdate• Trigger.isDelete• Trigger.isUndelete

fterefore

Page 10: Deep Dive into Apex Triggers

#forcewebinar

Why are Triggers hard and confusing?

• They are all Code

• They are shared across the instance

• They execute with everything else

(Workflows, Flows, Process Builder)

• They can cause Recursion

• They need to be Bulkified

Page 11: Deep Dive into Apex Triggers

#forcewebinar

Why do we need Triggers?

• Use triggers to perform complex tasks

that do not need user intervention.

• Execute Business rules without

worrying about security

• Examples• Send Data to External System on Save

• Update related and non related records

• Assignments based on business rules.

Page 12: Deep Dive into Apex Triggers

#forcewebinar

Considerations for Trigger Management

• Discuss with Architect or other Developers about

best practices

• Avoid writing Triggers when not needed

• Build Logical Trigger Switches

• Keep Logic outside of Triggers

• Consider having only one Trigger per Object

• Follow Apex Best Practices

• Build good Test classes

Page 13: Deep Dive into Apex Triggers

Best Practice Discuss with Architector Other Developers

Page 14: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Know the Standards, Naming

Conventions

• Discuss your approach with

others in your team

• If you don’t know, ASK

Page 15: Deep Dive into Apex Triggers

Best Practice Avoid Writing Triggers

when not needed

Page 16: Deep Dive into Apex Triggers

#forcewebinar

Use Case

• Create a Task when Lead Status

or Priority Changes

• Update a field “Next Step” on

Opportunity when Opportunity is

Closed Won

Page 17: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Consider using Workflow rules,

Flows, Process Builder or other

point & click tools before

considering building an Apex

Trigger

• Do not over-architect for simple

things

Page 18: Deep Dive into Apex Triggers

Best Practice Trigger Switches

Page 19: Deep Dive into Apex Triggers

#forcewebinar

Use case

• During a massive data load

exercise I do not want the

triggers to run.

• The similar upload is scheduled

on a weekly basis

• I do not want triggers to run when

Integration User is sending data.

Page 20: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Control the execution of Triggers

by using Custom Settings.

• Provide configurations wherever

possible to allow admin /

business users to control Trigger

Execution.

Page 21: Deep Dive into Apex Triggers

Best Practice Keep Logic outside of Triggers

Page 22: Deep Dive into Apex Triggers

#forcewebinar

Use case

• A Trigger on Task that is sending an email to a Group when Task status is changed or when it becomes overdue

• User needs to have ability to manually send an email to the Group for selected records.

Page 23: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Make the Triggers Logic-less • Use Handler/Helper classes that perform the

core business logic that can be reusable across the organization.

Trigger Apex Class

Handler

Controller

Page 24: Deep Dive into Apex Triggers

Best Practice One Trigger per Object

Page 25: Deep Dive into Apex Triggers

#forcewebinar

Use case

• A lead has 20+

Workflow rules

• 10 different Triggers

on Lead

• Assignment Rules

• Flows

Page 26: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Control the execution order

• Debugging simplified

• Separation of Concerns

• Structured and Organized

One Trigger to rule them all

Page 27: Deep Dive into Apex Triggers

Best Practice Good Test Classes

Page 28: Deep Dive into Apex Triggers

#forcewebinar

Best Practice

• Write tests for Bulk

Data, Always

• Test all your business

scenarios

• Use asserts, lavishly

Page 29: Deep Dive into Apex Triggers

Trigger Framework

Page 30: Deep Dive into Apex Triggers

#forcewebinar

Trigger Framework

• Design Patterns

• Observer/Listener

• Domain Model

• Factory Pattern

• Trigger Templates

Page 31: Deep Dive into Apex Triggers

Debugging Techniques

Page 32: Deep Dive into Apex Triggers

#forcewebinar

Developer Console

• Stack Tree

• Execution Overview

• Execution Logs

• Profiling

Page 33: Deep Dive into Apex Triggers

#forcewebinar

Logging Techniques

• Custom Log Class

• Push & Pop

• Custom Object Tracking

Page 35: Deep Dive into Apex Triggers

#forcewebinarChallenge Yourself! http://developer.salesforce.com/trailhead

• Interactive learning paths• Earn badges and points• Declarative and Programmatic

BRAND NEW! Introducing Trailhead

Page 36: Deep Dive into Apex Triggers

#forcewebinarChallenge Yourself! http://developer.salesforce.com/trailhead

Trailhead – Apex Triggers

Page 37: Deep Dive into Apex Triggers

#forcewebinar

New App Builders

http://www.udacity.com/salesforcehttp://ccoenraets.github.io/salesforce-

developer-workshop

Developer WorkshopFast Track Tutorial for

Experienced Developers

Experienced App Builders

More Free Online Tutorials

Page 38: Deep Dive into Apex Triggers

#forcewebinar

Upcoming Webinar

Building Mobile Apps onSalesforce Platform with Mobile SDK

June 24, 2015

https://developer.salesforce.com/calendar

Page 39: Deep Dive into Apex Triggers

#forcewebinar

Survey

Your feedback is crucial to the successof our webinar programs. Thank you!

http://bit.ly/1HM3hKo

Page 40: Deep Dive into Apex Triggers

Q & A

Page 41: Deep Dive into Apex Triggers

#forcewebinar

Jitendra ZaaSalesforce MVP@jitendrazaa

Abhinav GuptaSalesforce MVP@abhinavguptas

Q & A

Page 42: Deep Dive into Apex Triggers

Thank You