Making Your Apex and Visualforce Reusable

  • View

  • Download

Embed Size (px)


Have you ever had a functionality that was the same across multiple objects or fields, but needed to have it slightly different across each implementation? Join us as we show you how to build Visualforce pages in a more reusable manner. Using practical code examples, we'll be taking a code example from a page that is strongly typed to an object to using (1) a Visualforce Component and Dynamic Visualforce Field Bindings to make the page flexible, (2) Apex Describe methods to detect what type of object is being used, and (3) Custom Settings objects to maintain flexibility across objects. Always keep an eye out for opportunities to reuse old code without having to have multiple copies of code to update each time something changes!

Text of Making Your Apex and Visualforce Reusable

  • 1. Get the Most Out of Your Code Making Your Apex and Visualforce Reusable Raleigh Chen, USAA, Sr. Software Developer/Integrator
  • 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. Raleigh Chen Senior Software Developer/Integrator Enterprise Decision Support Systems
  • 4. Primary Problem VS
  • 5. What Makes Code Reusable? Flexibility Genericisation
  • 6. Case Study Example Two custom fields on two different objects similar functionality Campaign Source System Subsystem Application Lead IT System (IT Source of Lead) Lead Application (Lead Generation Application)
  • 7. Campaign Starting Point Visualforce Page for Campaign
  • 8. Campaign Create Component Generic Object Components allow for a level of flexibility/genericisation. Create new Visualforce Component Replace PageBlock with a Visualforce Component Tip: The tag allows you to pass values/objects from the visualforce page to the component
  • 9. COMPONENTIZATION replacing a Visualforce Page with a Component allowing for more flexibility due to the ability to embed Components in other Pages and pass variables into Components
  • 10. Campaign Add Component Controller Reusable Logic should live in the Generic Component Controller Create new Component Controller Replace the Attribute Object being passed with a more flexible Object ID Tip: The assignTo property in the tag allows you to pass values/objects from the Visualforce page directly to the components controller
  • 11. IDEEIFY passing an ID instead of an object or fields from an object to ensure portability across any object type
  • 12. Campaign Begin to Genericize Controller Use GlobalDescribe to allow different Objects Add describe methods necessary for Generic Use getGlobalDescribe() pulls all Object tokens getDescribe() pulls Object details getName() pulls Object Name getKeyPrefix() retrieves the 3 character Object Prefix Remove Strongly Typed fields from the Visualforce Component Tip: Using the Dynamic Visualforce Binding Notation runObject[fieldName] allows using variables for field names.
  • 13. DESCRIBESPLOSION using a generic ID to determine object metadata such as Object Name, key prefix, etc. without relying on hardcoded values
  • 14. DYNAMISUALFORDING using Dynamic Visualforce Binding notation to ensure flexibility in your sObjects field names so that they can be variablized and determined at runtime instead of being hardcoded
  • 15. Campaign Create Custom Setting Object Custom Settings allow for faster access to metadata for mappings, settings, environment variables, etc. Define a new Custom Setting Tip: Custom Settings prevent developers from having to alter code as it moves from environment to environment
  • 16. Campaign Add Campaign Entry to Custom Setting The entry will map the 3 character Object Prefix to all the Object specific information we need Define Object and Field Name fields Set Values for Campaign
  • 17. METAFIELD METANAMING METADATA storing the field names that are needed to refer to an object within another reference object, such as a Custom Setting so that the fields that are used can be changed and new possible objects added without code changes
  • 18. Campaign Finalize Controller Remove all mention of any specific Objects/Field Names Utilize Custom Setting to get Object/Field Names based on passed Record ID Tip: The getInstance() method of a Custom Setting allows for quick retrieval of a row of data based on the Name.
  • 19. DEPROPERIZE ensuring that there is no hardcoded object names or field names (except for the Custom Setting object and fields) to ensure that the component can be used with ANY object
  • 20. Lead Try it out for Lead! Create the same Page for Leads Add entry in Custom Settings for Lead Add Leads Visualforce Page
  • 21. Review Visualforce Components Describe Functions getGlobalDescribe() getDescribe() getName() getKeyPrefix()
  • 22. Review (cont.) Custom Settings Definition/Usage Custom_Setting_Object_Name.getInstance(rowName) Custom_Setting_Object_Name.getAll()
  • 23. Raleigh Chen Sr. Software Developer/Integrator, USAA