Design Patterns for Asynchronous Apex


Published on

Have you ever wanted to schedule an Apex operation to repeat every 10 minutes? To have one future call start another one? Is the 10 future call per context limit getting you down? Recent additions to the Apex language have opened the door to entirely new asynchronous design patterns. Join us to learn about the recent language changes, how they are used, and the kinds of design patterns they enable.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Design Patterns for Asynchronous Apex

  1. 1. Design Patterns for Asynchronous Apex Dan Appleman, Full Circle CRM, CTO Author: Advanced Apex Programming for and @danappleman
  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, 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 products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of, 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., inc. assumes no obligation and does not intend to update these forward-looking statements.
  3. 3. Our goal today… Explore how recent changes in Apex support new solutions to common asynchronous “challenges”
  4. 4. Quick review ▪Why do we use asynchronous code? ▪The big three: Future calls, Batch Apex and Scheduled Apex ▪The Rules ▪ Future can’t call future, 10 future calls per context ▪ Batch can’t call future, only 5 at once ▪ Limited # of scheduled Apex jobs ▪ Scheduled Apex job blocks updates
  5. 5. What would you like to do? ▪Future calling future ▪Chaining asynchronous operations ▪More frequent and finer resolution scheduled jobs ▪Anything else?
  6. 6. What’s new? ▪Spring 12: New type system ▪Winter 13: Start batch in the Finish statement of a batch ▪Summer 13: Increased # of Scheduled Apex job Scheduled batch ▪Winter 14: CronJobDetail object
  7. 7. Challenge: Scheduled Apex blocks updates Solution: Use type system to dynamically create classes that implement delegated functionality Platform Scheduled Class Dynamic creation Delegated Class
  8. 8. Challenge: Perform a series of batch operations Solution: Chain batch operations Code Start Execute Execute Finish Other batch
  9. 9. Challenge: Future call can’t call a future call Solution: Alternate future calls with scheduled Apex Future Scheduled This design pattern is problematic: •Can burn through limits •Potential concurrency issues Future
  10. 10. Challenge: Create more than 10 future calls in an execution context Solution: Replace future calls with custom async request object Use scheduled Apex or scheduled batch to process requests Insert objects Scheduled Batch “Future” “Future” “Future”
  11. 11. For more information Advanced Apex Programming for and – Second Edition • • • • • Incorporates changes through Winter 14 Designing for CPU Time limits Extended chapter on triggers Extended chapter on asynchronous patterns New chapter on concurrency patterns
  12. 12. All about Full Circle CRM Full Circle CRM develops native applications that enable marketers to accurately track and measure marketing campaign performance directly in Salesforce ▪ Bridge the information gap between leads and contacts ▪ Obtain complete and detailed campaign performance metrics ▪ Develop advanced campaign attribution and influence analytics Booth 1324 Moscone North
  13. 13. Download sample code for this session at