Adam Olshansky
Salesforce Developer,
Salesforce MVP
AdamToArchitect.com
@adam17amo
Writing Highly Genericized Code
Using Dynamic Apex and the
Tooling API
Adam Olshansky
Salesforce Developer, YouTube
@adam17amo
AdamToArchitect.com
NAMASTE
THANK YOU FOR INVITING
ME TO INDIA!
NAMASTE
Thank you to dQuotient for
sponsoring!
Agenda
• An overview of Generic code for Apex
• Salesforce Dynamic Coding Examples
• Tooling API
Generic Programming in Apex
Who are we talking to today?
• ISV developers
• Don’t know all conditions of customer orgs
• Individual app developers
• May need to accept input from clients/customers
• Any developer accepting user input
• Tailor your queries and results based on user conditions
What problem are we solving for?
● Rigid/Fragile
● Poor (Re)usability
● Lots of patches/quick fixes
● Duplicate code
● Overly complicated data structures
○ Maps of Maps of Maps of…..
○ Managing the data in a 5-D Arrays
2 Different Ways of Writing Code
​Static
​Dynamic/Generic
Different Ways of Writing Code: Static vs Dynamic
Static
• Generated at compile time
• Hard coded
• Doesn’t usually have dependencies
• Works well if you know what’s in the
org
• Most commonly used by developers at
a company developing for their own
org
Dynamic/Generic
• Generated at run time
• Dependent on user input
• May have org-specific dependencies
• Works well if you DON’T know what’s in the
org
• Most commonly used by developers whose
code needs to run in customized orgs
Tradeoffs of Dynamic Coding
Negatives
• More code to write
• More code to test
• Less readabillity
Positives
• More detailed information
• Less chance of an error down the road
• Endless possibilities
Dynamic Coding Examples
Tools of the trade
● Custom Settings/Metadata
● Schema class
● Dynamic Apex
● Dynamic Visualforce
● Dynamic SOQL
● Limits class
Dynamic Apex (Metadata)
Dynamic Apex (Data)
Dynamic Controller
Dynamic Visualforce
Dynamic SOQL
• Don’t always know what parameters are available in a user’s org
• In your org this works fine
Database.query()
Thanks to Dan Appleman, Advanced Apex Programming
• What if your customer doesn’t have record types defined for Lead
Limits Class
• Limits.getDMLRows();
• Limits.getDMLStatements();
• Limits.getHeapSize();
• Limits.getQueries();
• Limits.getQueryRows();
• bit.ly/apexlimitsclass (Apex Limits Class)
Dynamically make adjustments based on limits
Tying it Together
● You know how to design apps efficiently
● You know how to write generic code
● You know how to find information about org metadata
● What’s next?
Tooling API
An API for building...TOOLS
Tooling API Highlights
• Gather finely grained metadata about Apex classes, triggers, and tests in an org
• Classes, triggers, construction of symbol tables
• Query for code coverage, logs, SOQL results
• Query metadata about setup objects
• Business Processes, Flows, Custom items, Workflow components
• Can be utilized for source control/continuous integration
• Metadata containers, deployment details, code file metadata
• Can be queried through REST or SOAP
• Similar to Metadata API but with a ton of additional functionality
Great for building...tools!
Tooling API Examples
Create a class
https://andyinthecloud.com/category/tooling-api/
Additional Tooling API Use Cases
https://andyinthecloud.com/category/tooling-api/
• Show all objects that can be analyzed
• /services/data/v39.0/tooling/sobjects/
• Show list of all classes
• /services/data/v39.0/tooling/apexManifest
• Show all metadata about a class
• /services/data/v39.0/tooling/sobjects/ApexClass/01po0000004SL5b
Another Scenario
UTILIZE DYNAMIC APEX TO WRITE APEX USING APEX!
APEX-CEPTION!!!
Tools To Be Built
● UML Diagram Tool
● Symbol Table Visualizer
● Automated Testing Tools?
● Tools to identify unused code?
● Customize managed packages based
on org’s config
● Use your imagination!
Summary
Next Steps from Here
Summary and Next Steps
• Be thinking about the right type of code for the app you’re building
• Utilize design patterns
• Write dynamic code
• Gather granular details to build any app
• Get out there and build!
Where to Learn More
● Follow @andyinthecloud and his blog!
● Andy Fawcett’s Blog https://andyinthecloud.com/category/tooling-api/
● Andy and James Loghry’s Apex Tooling API Wrapper:
https://github.com/afawcett/apex-toolingapi/
● Pat Patterson’s Java Tooling API Wrapper: https://github.com/metadaddy/force-tooling-demo
https://developer.salesforce.com/blogs/developer-relations/2013/01/new-in-spring-13-the-too
ling-api.html
● Tom Patros VF page Utilizing Symbol Tables (The Documenter):
https://github.com/tompatros/df13-tooling-api-demo
Thank Y u

Write Generic Code with the Tooling API

  • 1.
    Adam Olshansky Salesforce Developer, SalesforceMVP AdamToArchitect.com @adam17amo Writing Highly Genericized Code Using Dynamic Apex and the Tooling API
  • 2.
    Adam Olshansky Salesforce Developer,YouTube @adam17amo AdamToArchitect.com
  • 3.
    NAMASTE THANK YOU FORINVITING ME TO INDIA!
  • 4.
    NAMASTE Thank you todQuotient for sponsoring!
  • 5.
    Agenda • An overviewof Generic code for Apex • Salesforce Dynamic Coding Examples • Tooling API
  • 6.
  • 7.
    Who are wetalking to today? • ISV developers • Don’t know all conditions of customer orgs • Individual app developers • May need to accept input from clients/customers • Any developer accepting user input • Tailor your queries and results based on user conditions
  • 8.
    What problem arewe solving for? ● Rigid/Fragile ● Poor (Re)usability ● Lots of patches/quick fixes ● Duplicate code ● Overly complicated data structures ○ Maps of Maps of Maps of….. ○ Managing the data in a 5-D Arrays
  • 9.
    2 Different Waysof Writing Code ​Static ​Dynamic/Generic
  • 10.
    Different Ways ofWriting Code: Static vs Dynamic Static • Generated at compile time • Hard coded • Doesn’t usually have dependencies • Works well if you know what’s in the org • Most commonly used by developers at a company developing for their own org Dynamic/Generic • Generated at run time • Dependent on user input • May have org-specific dependencies • Works well if you DON’T know what’s in the org • Most commonly used by developers whose code needs to run in customized orgs
  • 11.
    Tradeoffs of DynamicCoding Negatives • More code to write • More code to test • Less readabillity Positives • More detailed information • Less chance of an error down the road • Endless possibilities
  • 12.
  • 13.
    Tools of thetrade ● Custom Settings/Metadata ● Schema class ● Dynamic Apex ● Dynamic Visualforce ● Dynamic SOQL ● Limits class
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
    Dynamic SOQL • Don’talways know what parameters are available in a user’s org • In your org this works fine Database.query() Thanks to Dan Appleman, Advanced Apex Programming • What if your customer doesn’t have record types defined for Lead
  • 19.
    Limits Class • Limits.getDMLRows(); •Limits.getDMLStatements(); • Limits.getHeapSize(); • Limits.getQueries(); • Limits.getQueryRows(); • bit.ly/apexlimitsclass (Apex Limits Class) Dynamically make adjustments based on limits
  • 20.
    Tying it Together ●You know how to design apps efficiently ● You know how to write generic code ● You know how to find information about org metadata ● What’s next?
  • 21.
    Tooling API An APIfor building...TOOLS
  • 22.
    Tooling API Highlights •Gather finely grained metadata about Apex classes, triggers, and tests in an org • Classes, triggers, construction of symbol tables • Query for code coverage, logs, SOQL results • Query metadata about setup objects • Business Processes, Flows, Custom items, Workflow components • Can be utilized for source control/continuous integration • Metadata containers, deployment details, code file metadata • Can be queried through REST or SOAP • Similar to Metadata API but with a ton of additional functionality Great for building...tools!
  • 23.
    Tooling API Examples Createa class https://andyinthecloud.com/category/tooling-api/
  • 24.
    Additional Tooling APIUse Cases https://andyinthecloud.com/category/tooling-api/ • Show all objects that can be analyzed • /services/data/v39.0/tooling/sobjects/ • Show list of all classes • /services/data/v39.0/tooling/apexManifest • Show all metadata about a class • /services/data/v39.0/tooling/sobjects/ApexClass/01po0000004SL5b
  • 25.
    Another Scenario UTILIZE DYNAMICAPEX TO WRITE APEX USING APEX! APEX-CEPTION!!!
  • 26.
    Tools To BeBuilt ● UML Diagram Tool ● Symbol Table Visualizer ● Automated Testing Tools? ● Tools to identify unused code? ● Customize managed packages based on org’s config ● Use your imagination!
  • 27.
  • 28.
    Summary and NextSteps • Be thinking about the right type of code for the app you’re building • Utilize design patterns • Write dynamic code • Gather granular details to build any app • Get out there and build!
  • 29.
    Where to LearnMore ● Follow @andyinthecloud and his blog! ● Andy Fawcett’s Blog https://andyinthecloud.com/category/tooling-api/ ● Andy and James Loghry’s Apex Tooling API Wrapper: https://github.com/afawcett/apex-toolingapi/ ● Pat Patterson’s Java Tooling API Wrapper: https://github.com/metadaddy/force-tooling-demo https://developer.salesforce.com/blogs/developer-relations/2013/01/new-in-spring-13-the-too ling-api.html ● Tom Patros VF page Utilizing Symbol Tables (The Documenter): https://github.com/tompatros/df13-tooling-api-demo
  • 30.