Punta Dreamin 17 Generic Apex and Tooling Api

27
@puntadreamin #PD17

Transcript of Punta Dreamin 17 Generic Apex and Tooling Api

Page 1: Punta Dreamin 17 Generic Apex and Tooling Api

@puntadreamin #PD17

Page 2: Punta Dreamin 17 Generic Apex and Tooling Api

Writing Highly Genericized Code Using Dynamic Apex and the Tooling API

Adam Olshansky Salesforce Developer, Contractor at YouTube Salesforce MVP, 13X Certified AdamToArchitect.com @adam17amo

Page 3: Punta Dreamin 17 Generic Apex and Tooling Api

¡Estoy feliz de estar en Punta del Este!

Page 4: Punta Dreamin 17 Generic Apex and Tooling Api

Agenda

•  Overview of generic code for Apex •  Dynamic Code in Salesforce •  Tooling API

Page 5: Punta Dreamin 17 Generic Apex and Tooling Api

Generic Programming in Apex

Page 6: Punta Dreamin 17 Generic Apex and Tooling Api

What problems are we solving for?

•  Rigid or fragile code •  Poor Reusability •  Lots of patches/quick fixes •  Duplicate code •  Overly complicated data structures •  Maps of maps of maps…

Page 7: Punta Dreamin 17 Generic Apex and Tooling Api

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

Page 8: Punta Dreamin 17 Generic Apex and Tooling Api

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

Page 9: Punta Dreamin 17 Generic Apex and Tooling Api

2 Different Ways of Writing Code

Page 10: Punta Dreamin 17 Generic Apex and Tooling Api

Tradeoffs

 Positives

•  More detailed information

•  Less chance of an error down the road

•  Endless possibilities

 Negatives

•  More code to write

•  More code to test

•  Less readability

Page 11: Punta Dreamin 17 Generic Apex and Tooling Api

Tools of the Trade

Page 12: Punta Dreamin 17 Generic Apex and Tooling Api

Several Ways in Salesforce

•  Custom Settings/Metadata

•  Dynamic Apex

•  Dynamic Visualforce

•  Dynamic SOQL

•  Limits class

•  Schema class

Page 13: Punta Dreamin 17 Generic Apex and Tooling Api

Dynamic Apex

Page 14: Punta Dreamin 17 Generic Apex and Tooling Api

Dynamic Controller

Page 15: Punta Dreamin 17 Generic Apex and Tooling Api

Dynamic Visualforce

Page 16: Punta Dreamin 17 Generic Apex and Tooling Api

Dynamic SOQL

•  Don’t always know what parameters are available in a user’s org

•  In your org this will work

•  What if your customer doesn’t have record types defined for Lead?

Page 17: Punta Dreamin 17 Generic Apex and Tooling Api

Limits Class

•  Limits.getDMLRows();

•  Limits.getDMLStatements();

•  Limits.getHeapSize();

•  Limits.getQueries();

•  Limits.getQueryRows();

•  bit.ly/apexlimitsclass (Apex Limits Class)

Page 18: Punta Dreamin 17 Generic Apex and Tooling Api

Tooling API An API for building…Tools

Page 19: Punta Dreamin 17 Generic Apex and Tooling Api

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!

Page 20: Punta Dreamin 17 Generic Apex and Tooling Api

Our Scenario

 UTILIZE DYNAMIC APEX TO WRITE APEX USING APEX!

 APEX-CEPTION!!!

Page 21: Punta Dreamin 17 Generic Apex and Tooling Api

Additional Tooling API Use Cases

•  Show all objects that can be analyzed

/services/data/v37.0/tooling/sobjects/

•  Show list of all classes

/services/data/v37.0/tooling/apexManifest

•  Show all metadata about a class

/services/data/v37.0/tooling/sobjects/ApexClass/01po0000004SL5b

Page 22: Punta Dreamin 17 Generic Apex and Tooling Api

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!

Page 23: Punta Dreamin 17 Generic Apex and Tooling Api

Summary Next Steps from Here

Page 24: Punta Dreamin 17 Generic Apex and Tooling Api

Summary and Next Steps

•  Be thinking about the right type of code for the app you’re building

•  Write dynamic code

•  Gather granular details to build any app

•  Get out there and build!

Page 25: Punta Dreamin 17 Generic Apex and Tooling Api

Where to Learn More

•  Follow @andyinthecloud (Andy Fawcett, CTO Financial Force)

•  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-tooling-api.html

•  Tom Patros VF page Utilizing Symbol Tables: https://github.com/tompatros/df13-tooling-api-demo and http://www.redargyle.com/blog/tooling-around-tooling-api/

Page 26: Punta Dreamin 17 Generic Apex and Tooling Api

Thank Y u

Page 27: Punta Dreamin 17 Generic Apex and Tooling Api

Q&A