Apex Design Patterns

45
Apex Design Patterns Developers Richard Vanhook and Dennis Thong @richardvanhook @denniscfthong

description

Apex allows you to build just about any custom solution on the Force.com platform. But what are the common design patterns and associated best practices for Apex development, and how can you leverage these patterns and best practices to avoid reinventing the wheel? Join us to learn some common Force.com design patterns and walk through sample code for best implementing them.

Transcript of Apex Design Patterns

Page 1: Apex Design Patterns

Apex Design PatternsDevelopers

Richard Vanhook and Dennis Thong@richardvanhook @denniscfthong

Page 2: Apex Design Patterns

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 Design Patterns

Richard VanhookSenior Technical Solution Architect@richardvanhook

Dennis ThongDirector - Technical Solution Architect@denniscfthong

Page 4: Apex Design Patterns

The PlanCover four design patterns

▪ Problem▪ Pattern Abstract▪ Code

Your participation is encouraged!

Page 5: Apex Design Patterns

Patterns1. ?2. ?3. ?4. ?

Page 6: Apex Design Patterns

Meet Billy, Your Admin• 3+ yrs as Salesforce Admin• Just recently started coding• Sad because of this:

• Needs your help!

Trigger.AccountTrigger: line 3, column 1System.LimitException: Too many record type describes: 101

Page 7: Apex Design Patterns

The Offending Code

• When a single Account is inserted, what happens?• When 200+ Accounts are inserted…?

trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = new AccountFooRecordType(); .... }}

010203040506

public class AccountFooRecordType { public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); }}

07080910111213

Page 8: Apex Design Patterns

Solution: Singleton

Singleton

- instance : Singleton

- Singleton()+ getInstance() : Singleton

Page 9: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = new AccountFooRecordType(); .... }}

010203040506

public class AccountFooRecordType { public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); }}

07080910111213

• Above is the offending code again• Changes are highlighted in next slides

Page 10: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = (new AccountFooRecordType()).getInstance(); .... }}

010203040506

public class AccountFooRecordType { public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public AccountFooRecordType getInstance(){

return new AccountFooRecordType(); }}

07080910111213141516

Page 11: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = AccountFooRecordType.getInstance(); .... }}

010203040506

public class AccountFooRecordType { public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public static AccountFooRecordType getInstance(){

return new AccountFooRecordType(); }}

07080910111213141516

Page 12: Apex Design Patterns

Static▪ Can modify member variables, methods, and blocks▪ Executed when?

• When class is introduced (loaded) to runtime environment

▪ How long does that last in Java?• Life of the JVM

▪ In Apex?• Life of the transaction

Page 13: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = AccountFooRecordType.getInstance(); .... }}

010203040506

public class AccountFooRecordType { public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public static AccountFooRecordType getInstance(){

return new AccountFooRecordType(); }}

07080910111213141516

Page 14: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = AccountFooRecordType.getInstance(); .... }}

010203040506

public class AccountFooRecordType { public static AccountFooRecordType instance = null; public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public static AccountFooRecordType getInstance(){ if(instance == null) instance = new AccountFooRecordType(); return instance; }}

070809101112131415161718

Page 15: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = AccountFooRecordType.getInstance(); .... }}

010203040506

public class AccountFooRecordType { private static AccountFooRecordType instance = null; public String id {get;private set;} public AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public static AccountFooRecordType getInstance(){ if(instance == null) instance = new AccountFooRecordType(); return instance; }}

070809101112131415161718

Page 16: Apex Design Patterns

Let’s Code It!trigger AccountTrigger on Account (before insert, before update) { for(Account record : Trigger.new){ AccountFooRecordType rt = AccountFooRecordType.getInstance(); .... }}

010203040506

public class AccountFooRecordType { private static AccountFooRecordType instance = null; public String id {get;private set;} private AccountFooRecordType(){ id = Account.sObjectType.getDescribe() .getRecordTypeInfosByName().get('Foo').getRecordTypeId(); } public static AccountFooRecordType getInstance(){ if(instance == null) instance = new AccountFooRecordType(); return instance; }}

070809101112131415161718

Page 17: Apex Design Patterns

Patterns1. Singleton2. ?3. ?4. ?

Page 18: Apex Design Patterns

geocode('moscone center') //=> 37.7845935,-122.3994262

Page 19: Apex Design Patterns

Your Suggestion to Billypublic class GoogleMapsGeocoder{ public static List<Double> getLatLong(String address){ //web service callout of some sort return new List<Double>{0,0}; }}

010203040506

But then Billy mentions these requirements▪ Need other options besides Google Maps▪ Allow client code to choose provider

System.debug(GoogleMapsGeocoder.getLatLong('moscone center'));//=> 13:56:36.029 (29225000)|USER_DEBUG|[29]|DEBUG|(0.0, 0.0)

0708

Page 20: Apex Design Patterns

Solution: Strategy

defines a family of algorithms, encapsulates each one, and makes them interchangeable▪ Context => Geocoder▪ operation() => getLatLong()▪ Strategy => GeocodeService▪ ConcreteStrategyA => GoogleMapsImpl▪ ConcreteStrategyB => MapQuestImpl

Context

+operation()

Strategy

+operation()

ConcreteStrategyA

+operation()

Clientstrategies

*1

ConcreteStrategyB

+operation()

Page 21: Apex Design Patterns

Let’s Code Itpublic interface GeocodeService{ List<Double> getLatLong(String address);}

010203

public class GoogleMapsImpl implements GeocodeService{ public List<Double> getLatLong(String address){ return new List<Double>{0,0}; }}

public class MapQuestImpl implements GeocodeService{ public List<Double> getLatLong(String address){ return new List<Double>{1,1}; }}

0405060708

0910111213

Page 22: Apex Design Patterns

public class Geocoder { private GeocodeService strategy; public Geocoder(GeocodeService s){ strategy = s; } public List<Double> getLatLong(String address){ return strategy.getLatLong(address); }}

010203040506070809

Geocoder geocoder = new Geocoder(new GoogleMapsImpl());System.debug(geocoder.getLatLong('moscone center'));//=> 13:56:36.029 (29225000)|USER_DEBUG|[29]|DEBUG|(0.0, 0.0)

101112

Geocoder

+ Geocoder(GeocodeService)

+getLatLong(String)

GeocodeService

+getLatLong(String)

GoogleMapsImpl

+getLatLong(String)

Clientstrategies

*1

MapQuestImpl

+getLatLong(String)

Page 23: Apex Design Patterns

public class Geocoder { public static final Map<String,Strategy> strategies = new Map<String,Strategy>{'googlemaps' => new GoogleMapsImpl() ,'mapquest' => new MapQuestImpl()}; private GeocodeService strategy; public Geocoder(String name){ strategy = strategies.get(name);} public List<Double> getLatLong(String address){ return strategy.getLatLong(address); }}

01020304050607080910

Geocoder geocoder = new Geocoder('googlemaps');System.debug(geocoder.getLatLong('moscone center'));//=> 13:56:36.029 (29225000)|USER_DEBUG|[29]|DEBUG|(0.0, 0.0)

111213

Geocoder

+ Geocoder(String)

+getLatLong(String)

GeocodeService

+getLatLong(String)

GoogleMapsImpl

+getLatLong(String)

Clientstrategies

*1

MapQuestImpl

+getLatLong(String)

Page 24: Apex Design Patterns

public class Geocoder { public class NameException extends Exception{} public static final Map<String,GeocodeService> strategies; static{ GlobalVariable__c gv = GlobalVariable__c.getInstance('strategies'); List<String> strategyNames = new List<String>(); if(gv != null && gv.value__c != null) strategyNames = gv.value__c.split(','); strategies = new Map<String,GeocodeService>(); for(String name : strategyNames){ try{strategies.put(name,(GeocodeService)Type.forName(name+'impl').newInstance());} catch(Exception e){continue;} //skip bad name silently } } private GeocodeService strategy; public Geocoder(String name){ if(!strategies.containsKey(name)) throw new NameException(name); strategy = strategies.get(name); } public List<Double> getLatLong(String address){ return strategy.getLatLong(address); }}

01020304050607080910111213141516171819202122

Page 25: Apex Design Patterns

Patterns1. Singleton2. Strategy3. ?4. ?

Page 26: Apex Design Patterns

Question

Transient selection checkboxes

Calculated Fields with Updates

How do we extend the functionality of an sObject in Apex without adding new fields?

Page 27: Apex Design Patterns

Solution – sObject Decorator

(aka Wrapper Classes v2.0)

Page 28: Apex Design Patterns

Example - ScenarioWeather sObject

▪ City__c▪ TempInFahrenheit__c (in Fahrenheit)

VisualForce Requirements▪ Stored temperature in Fahrenheit is displayed in Celcius▪ User enters Temperature in Celcius and is stored to the record in Fahrenheit

Bi-directional Display and Calculation

Page 29: Apex Design Patterns

The Code – Decorated sObject Classpublic class DecoratedWeather {

public Weather__c weather { get; private set; }

public DecoratedWeather ( Weather__c weather) { this.weather = weather;

}

public Decimal tempInCelcius { get {

if (weather != null && tempInCelcius == null )tempInCelcius = new Temperature().FtoC(weather.TempInFahrenheit__c);

return tempInCelcius;

} set {

if (weather != null && value != null ) weather.TempInFahrenheit__c= new Temperature().CtoF(value);

tempInCelcius = value;}

}}

0102030405060708091011121314151617181920212223

Page 30: Apex Design Patterns

The Code – Custom Controllerpublic class Weather_Controller {

public List<DecoratedWeather> listOfWeather {

get { if (listOfWeather == null) {

listOfWeather = new List<DecoratedWeather>(); for (Weather__c weather : [select name, temperature__c from Weather__c]) {

listOfWeather.add(new DecoratedWeather(weather)); }

} return listOfWeather;

} private set;

}}

010203040506070809101112131415161718

Page 31: Apex Design Patterns

The Code – VisualForce Page<apex:page controller="weather_controller">

<!-- VF page to render the weather records with Ajax that only rerenders the page on change of the temperature

--> <apex:form id="theForm"> <apex:pageBlock id="pageBlock"> <apex:pageBlockTable value="{!listOfWeather}" var="weather"> <apex:column value="{!weather.weather.name}"/> <apex:column headerValue="Temperature (C)"> <apex:actionRegion > <apex:inputText value="{!weather.tempInCelcius}"> <apex:actionSupport event="onchange" reRender="pageBlock"/> </apex:inputText> </apex:actionRegion> </apex:column> <apex:column headerValue="Temperature (F)" value="{!weather.weather.Temperature__c}" id="tempInF"/> </apex:pageBlockTable>

</apex:pageBlock> </apex:form></apex:page>

010203040506070809101112131415161718192021222324 No client side

logic!!!

Page 32: Apex Design Patterns

Finally, on the VisualForce page

Page 33: Apex Design Patterns

Patterns1. Singleton2. Strategy3. sObject Decorator4. ?

Page 34: Apex Design Patterns

Billy Has a New Problem!Billy wrote a trigger to create an order on close of an opportunity,

however:▪ It always creates a new order every time the closed opportunity is

updated▪ When loading via Data Loader, not all closed opportunities result in an

order

Page 35: Apex Design Patterns

The Offending Codetrigger OpptyTrigger on Opportunity (after insert, after update) {

if (trigger.new[0].isClosed) {Order__c order = new Order__c();…insert order

}}

0102030405060708

No Bulk Handling

Occurs regardless of prior state

Poor reusability

Page 36: Apex Design Patterns

Any Better?trigger OpptyTrigger on Opportunity (after insert, after update) {

new OrderClass().CreateOrder(trigger.new);

}

0102030405

public class OrderClass {

public void CreateOrder(List<Opportunity> opptyList) {for (Opportunity oppty : opptyList) {

if (oppty.isClosed) {Order__c order = new Order__c();...insert order;

}}

}}

01020304050607080910111213

Occurs regardless of prior state

Not bulkified

Page 37: Apex Design Patterns

How’s this?trigger OpptyTrigger on Opportunity (before insert, before update) {

if (trigger.isInsert) {new OrderClass().CreateOrder(trigger.newMap, null);

elsenew OrderClass().CreateOrder(trigger.newMap, trigger.oldMap);

010203040506

public class OrderClass {

public void CreateOrder(Map<Opportunity> opptyMapNewMap<Opportunity> opptyMapOld) {

List<Order__c> orderList = new List<Order__c>();for (Opportunity oppty : opptyMapNew.values()) {

if (oppty.isClosed && (opptyMapOld == null ||!opptyMapOld.get(oppty.id).isClosed)) {

Order__c order = new Order__c();...orderList.add(order);

}}insert orderList ;

}

010203040506070809101112131415

This code is highly coupled and not reusable

At least it’s bulkified

Page 38: Apex Design Patterns

Solution – Bulk Transition

• Checks for eligible records that have changed state and match criteria

• Calls utility class method to perform the work

• Only eligible records are passed in

• Generic utility class method that can be called from any context

Page 39: Apex Design Patterns

At Last!!!trigger OpptyTrigger on Opportunity (after insert, after update) {

if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate)) {List<Opportunity> closedOpptyList = new List<Opportunity>();for (Opportunity oppty : trigger.new) {

if (oppty.isClosed && (trigger.isInsert || (trigger.isUpdate &&!trigger.oldMap.get(oppty.id).isClosed)))

closedOpptyList.add(oppty);}if (!closedOpptyList.isEmpty())

new OrderClass().CreateOrderFromOpptys(closedOpptyList)

0102030405060708091011

public class OrderClass {public void CreateOrdersFromOpptys(List<Opportunity> opptyList) {

List<Order__c> orderList = new List<Order__c>();for (Opportunity oppty : opptyList) {

Order__c order = new Order__c();...orderList.add(order);

}insert orderList ;

010203040506070809

Trigger handles state transition

This method is now a lot more reusable and is bulk-

safe

Page 40: Apex Design Patterns

Patterns1. Singleton2. Strategy3. sObject Decorator4. Bulk State Transition

Page 41: Apex Design Patterns

ResourcesWrapper Classes - http://wiki.developerforce.com/page/Wrapper_Class Apex Code Best Practices - http://wiki.developerforce.com/page/Apex_Code_Best_Practices Apex Web Services and Callouts - http://wiki.developerforce.com/page/Apex_Web_Services_and_Callouts Sample Code - https://github.com/richardvanhook/Force.com-Patterns

Page 42: Apex Design Patterns

More at #DF13Apex Enterprise Patterns: Building Strong FoundationsTuesday, November 19th: 12:15 pm - 01:00 pm - Moscone Center West – 2009High Reliability DML and Concurrency Design Patterns for ApexMonday, November 18th: 11:15 am - 12:00 pm - Moscone Center West – 2009Making Your Apex and Visualforce ReusableTuesday, November 19th: 12:15 pm - 01:00 pm - Moscone Center West – 2022Design Patterns for Asynchronous ApexTuesday, November 19th: 05:15 pm - 06:00 pm - Moscone Center West – 2024The Apex Ten CommandmentsWednesday, November 20th: 04:00 pm - 04:45 pm - Moscone Center West - 2006 / 2008

Page 43: Apex Design Patterns

Speaker Name

Dennis Thong,@denniscfthong

Speaker Name

Richard Vanhook,@richardvanhook

Page 44: Apex Design Patterns

We want to hear from YOU!

Please take a moment to complete our session survey

Surveys can be found in the “My Agenda” portion of the Dreamforce app

Page 45: Apex Design Patterns