0
Get the Most Out of Your Code
Making Your Apex and Visualforce Reusable
Raleigh Chen, USAA, Sr. Software Developer/Integra...
Safe harbor
Safe harbor statement under the Private Securities Litigation Reform Act of 1995:
This presentation may contai...
Raleigh Chen
Senior Software Developer/Integrator
Enterprise Decision Support Systems
Primary Problem

VS
What Makes Code Reusable?

Flexibility

Genericisation
Case Study Example
Two custom fields on two different objects – similar functionality
▪ Campaign
• Source System
• Subsyst...
Campaign – Starting Point
Visualforce Page for Campaign
Campaign – Create Component
Generic Object Components allow for a level of
flexibility/genericisation.
▪ Create new Visual...
COMPONENTIZATION
“replacing a Visualforce Page with a Component allowing for
more flexibility due to the ability to embed ...
Campaign – Add Component Controller
Reusable Logic should live in the Generic Component Controller
▪ Create new Component ...
IDEEIFY
“passing an ID instead of an object or fields from an object to
ensure portability across any object type”
Campaign – Begin to Genericize Controller
Use GlobalDescribe to allow different Objects
▪ Add describe methods necessary f...
DESCRIBESPLOSION
“using a generic ID to determine object metadata such as
Object Name, key prefix, etc. without relying on...
DYNAMISUALFORDING
“using Dynamic Visualforce Binding notation to ensure flexibility
in your sObject’s field names so that ...
Campaign – Create Custom Setting Object
Custom Settings allow for faster access to metadata for mappings,
settings, enviro...
Campaign – Add Campaign Entry to Custom Setting
The entry will map the 3 character Object Prefix to all the Object
specifi...
METAFIELD METANAMING METADATA
“storing the field names that are needed to refer to an object
within another reference obje...
Campaign – Finalize Controller
Remove all mention of any specific Objects/Field Names
▪ Utilize Custom Setting to get Obje...
DEPROPERIZE
“ensuring that there is no hardcoded object names or field
names (except for the Custom Setting object and fie...
Lead – Try it out for Lead!
Create the same Page for Leads
▪ Add entry in Custom Settings for Lead
▪ Add Leads Visualforce...
Review
Visualforce Components
▪

<c:component_name attribute_name=“{!value}” />

▪

<apex:attribute name="objID" descripti...
Review (cont.)
Custom Settings
▪

Definition/Usage

▪

Custom_Setting_Object_Name.getInstance(rowName)

▪

Custom_Setting_...
Raleigh Chen
Sr. Software
Developer/Integrator,
USAA
Chatter Challenge
On Chatter, Submit an example of this type of genericisation using
the new Fieldset.
Winner gets a gold ...
Making Your Apex and Visualforce Reusable
Upcoming SlideShare
Loading in...5
×

Making Your Apex and Visualforce Reusable

990

Published on

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!

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
990
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Making Your Apex and Visualforce Reusable"

  1. 1. Get the Most Out of Your Code Making Your Apex and Visualforce Reusable Raleigh Chen, USAA, Sr. Software Developer/Integrator
  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 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.
  3. 3. Raleigh Chen Senior Software Developer/Integrator Enterprise Decision Support Systems
  4. 4. Primary Problem VS
  5. 5. What Makes Code Reusable? Flexibility Genericisation
  6. 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. 7. Campaign – Starting Point Visualforce Page for Campaign
  8. 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 <apex:attribute> tag allows you to pass values/objects from the visualforce page to the component
  9. 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. 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 <apex:attribute> tag allows you to pass values/objects from the Visualforce page directly to the component’s controller
  11. 11. IDEEIFY “passing an ID instead of an object or fields from an object to ensure portability across any object type”
  12. 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. 13. DESCRIBESPLOSION “using a generic ID to determine object metadata such as Object Name, key prefix, etc. without relying on hardcoded values”
  14. 14. DYNAMISUALFORDING “using Dynamic Visualforce Binding notation to ensure flexibility in your sObject’s field names so that they can be variablized and determined at runtime instead of being hardcoded”
  15. 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. 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. 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. 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. 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. 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. 21. Review Visualforce Components ▪ <c:component_name attribute_name=“{!value}” /> ▪ <apex:attribute name="objID" description="The object ID" required="true" type="String" assignTo="{! objectID}“ Dynamic Visualforce Field Bindings ▪ <apex:inputField value="{!object_name[field_name_variable]}" /> Describe Functions ▪ getGlobalDescribe() ▪ getDescribe() ▪ getName() ▪ getKeyPrefix()
  22. 22. Review (cont.) Custom Settings ▪ Definition/Usage ▪ Custom_Setting_Object_Name.getInstance(rowName) ▪ Custom_Setting_Object_Name.getAll()
  23. 23. Raleigh Chen Sr. Software Developer/Integrator, USAA
  24. 24. Chatter Challenge On Chatter, Submit an example of this type of genericisation using the new Fieldset. Winner gets a gold star.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×