25
Get the Most Out of Your Code Making Your Apex and Visualforce Reusable Raleigh Chen, USAA, Sr. Software Developer/Integrator

Making Your Apex and Visualforce Reusable

Embed Size (px)

DESCRIPTION

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!

Citation preview

Page 1: Making Your Apex and Visualforce Reusable

Get the Most Out of Your CodeMaking Your Apex and Visualforce Reusable

Raleigh Chen, USAA, Sr. Software Developer/Integrator

Page 2: Making Your Apex and Visualforce Reusable

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: Making Your Apex and Visualforce Reusable

Raleigh ChenSenior Software Developer/Integrator Enterprise Decision Support Systems

Page 4: Making Your Apex and Visualforce Reusable

Primary Problem

VS

Page 5: Making Your Apex and Visualforce Reusable

What Makes Code Reusable?

Flexibility Genericisation

Page 6: Making Your Apex and Visualforce Reusable

Case Study ExampleTwo 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)

Page 7: Making Your Apex and Visualforce Reusable

Campaign – Starting PointVisualforce Page for Campaign

Page 8: Making Your Apex and Visualforce Reusable

Campaign – Create ComponentGeneric 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

Page 9: Making Your Apex and Visualforce Reusable

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”

Page 10: Making Your Apex and Visualforce Reusable

Campaign – Add Component ControllerReusable 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

Page 11: Making Your Apex and Visualforce Reusable

IDEEIFY

“passing an ID instead of an object or fields from an object to ensure portability across any object type”

Page 12: Making Your Apex and Visualforce Reusable

Campaign – Begin to Genericize ControllerUse 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.

Page 13: Making Your Apex and Visualforce Reusable

DESCRIBESPLOSION

“using a generic ID to determine object metadata such as Object Name, key prefix, etc. without relying on hardcoded values”

Page 14: Making Your Apex and Visualforce Reusable

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”

Page 15: Making Your Apex and Visualforce Reusable

Campaign – Create Custom Setting ObjectCustom 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

Page 16: Making Your Apex and Visualforce Reusable

Campaign – Add Campaign Entry to Custom SettingThe 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

Page 17: Making Your Apex and Visualforce Reusable

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”

Page 18: Making Your Apex and Visualforce Reusable

Campaign – Finalize ControllerRemove 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.

Page 19: Making Your Apex and Visualforce Reusable

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”

Page 20: Making Your Apex and Visualforce Reusable

Lead – Try it out for Lead!Create the same Page for Leads

▪ Add entry in Custom Settings for Lead

▪ Add Leads Visualforce Page

Page 21: Making Your Apex and Visualforce Reusable

ReviewVisualforce 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()

Page 22: Making Your Apex and Visualforce Reusable

Review (cont.)Custom Settings

▪ Definition/Usage▪ Custom_Setting_Object_Name.getInstance(rowName)▪ Custom_Setting_Object_Name.getAll()

Page 23: Making Your Apex and Visualforce Reusable

Raleigh Chen

Sr. Software Developer/Integrator,

USAA

Page 24: Making Your Apex and Visualforce Reusable

Chatter ChallengeOn Chatter, Submit an example of this type of genericisation using the new Fieldset.

Winner gets a gold star.

Page 25: Making Your Apex and Visualforce Reusable