Você está na página 1de 35

Apex Code Analysis using the

Tooling API and Canvas

Andrew Fawcett, FinancialForce.com, CTO


@andyinthecloud
All about FinancialForce.com
Revolutionizing the Back Office
#1 Accounting, Billing and PSA Apps on the Salesforce platform

 Native apps
 San Francisco HQ, 595 Market St
 R&D in San Francisco, Harrogate UK, and Granada ES
 We are hiring! Meet us at Rehab!
Introduction

Why do I need to know more about my Apex code?


 Its hard to see code complexity from within the trenches
• Helps those unfamiliar learn complex code bases
 Tightly coupled code is harder to maintain and evolve

Goals of this Session


• What are the technologies that can help?
• Understand how to analyze your code with the Tooling API?
• Provide a take away demo of the Tooling API you can extend
Canvas: UI Integration with Salesforce

Provides a means of extending the Salesforce User Interface


 Chatter Tab
 Visualforce Tabs
 Publisher Actions
 Other areas see Canvas Developer Guide
Open to any new or existing external web page
 Pages can be developed and hosted within any web platform
 Developer SDK’s for Java and JavaScript are provided
 Pages must follow a specific authentication flow
Where do Canvas apps appear in Salesforce?
Chatter Tab
Where do Canvas apps appear in Salesforce?
Visualforce Page
Where do Canvas apps appear in Salesforce?
Publisher Action
Canvas Architecture
Access Method: Signed Request (Recommended)

Canvas aware Web Site


Salesforce User Interface (hold consumer secret)
(holds consumer secret) https://mysite.com/mypage
Initial HTTP POST passing signed_request
1. Receives HTTP POST, decodes
and validates request
Canvas Frame 2. Stores CanvasRequest and
User Web Page Interactions
presents page to user
3. Optionally calls back to Salesforce
via API’s using oAuth token

Salesforce API Interactions (using oAuth Token from CanvasRequest)


How do I make my web page Canvas aware?

Two Salesforce SDK’s


 Salesforce Canvas JavaScript SDK
 Salesforce Canvas Java SDK
• Includes JavaScript SDK

Handle Decoding and Parsing of the “CanvasRequest”


 Sent to the page when the user clicks in the Salesforce UI
 HTTP POST to the page via ‘signed_request’ parameter
• Contains amongst other information, oAuth token for Salesforce API access!
What is the Tooling API?

What: Manage Apex Code and Pages on the Platform


 More granular API than Metadata API built for …
• Building alternative IDE’s (Integrated Developer Environment)
– MavensMate
– Force.com IDE
• Build development tools
– Tools that perform further analysis on code via Symbol Table
What is the Tooling API?
 Use REST API bindings if you’re using a language that isn’t strongly typed, like
JavaScript.
 Use SOAP API bindings if you’re using a strongly typed language like Java that
generates Web service client code.
Tooling API Architecture and Objects

New Objects in the Salesforce Database


 Creating, querying, updating and deleting records
• NOTE: Only via Tooling API CRUD operations
 MetadataContainer Object
• Think, “Workspace” in your IDE for files being worked on
Tooling API Architecture and Objects

New Objects in the Salesforce Database


 Key Objects are ….

Use without MetadataContainer Use with MetadataContainer


• ApexClass • ApexClassMember
• ApexPage • ApexPageMember
• ApexComponent • ApexComponentMember
• ApexTrigger • ApexTriggerMember
What is a Symbol Table?

Child of ApexClass, ApexClassMember and ApexTriggerMember


 Variables
 Methods
 Inner Classes
 External References
• Lists references to the above from other Apex Classes and Apex Triggers
• NOTE: Only available after a compile!
Birds Eye View : Symbol Table Object

Only available after an Apex


compilation!
Apex UML Canvas Application: Demo

NOTE: Code shown is from my “Building Strong Foundation: Apex Enterprise Patterns” session
Apex UML Canvas Application: Architecture

 Hosted on Heroku
 Jetty Web Server
• Java Spring MVC Framework
• SOAP Tooling API (via JAX-WS)
– via wsimport Maven plugin

 Web Page
• jQuery
• UMLCanvas JS Library
 Maven Build System
Configuring a Canvas Application in Salesforce
 Makes HTTP POST to URL https://localhost:8443/app/canvas
• Note: /app/ is managed by Spring MVC and forwards to Java Controllers…
 Setup > Create > Applications
Integrating the Canvas SDK with Spring MVC

CanvasController.java
 Handles the HTTP POST made by Salesforce to /canvas
 Uses Salesforce Canvas SDK to decode and store in HTTP session
@Controller
@RequestMapping("/canvas")
public class CanvasController {

@RequestMapping(method = RequestMethod.POST)
public String canvasRequest(@RequestParam("signed_request") String signedRequest, HttpSession session)
{
String secret = System.getenv("CANVAS_CONSUMER_SECRET");
CanvasRequest request = SignedRequest.verifyAndDecode(signedRequest, secret);
session.setAttribute("canvasRequest", request);
return "redirect:umlcanvas";
}
}
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java
 Redirection from /canvas to /umlcanvas
 Page load, rendered by umlcanvas.jsp
@Controller
@RequestMapping("/umlcanvas")
public class UmlCanvasController {

@RequestMapping(method = RequestMethod.GET)
public String load(HttpSession session, Map<String, Object> map) throws Exception
{
// List classes on the page
ToolingAPIConnection toolingAPI = createToolingAPIConnection(session);
ApexClass[] apexClasses =
toolingAPI.service.query(
"SELECT Id, Name, SymbolTable " +
"FROM ApexClass"
, toolingAPI.session).getRecords().toArray(new ApexClass[0]);
for(ApexClass apexClass : apexClasses)
Apex UML Canvas : Code Walkthrough
umlcanvas.jsp
 Java Servlet Page (JSP)
 AJAX calls to
UmlCanvasController.java
 JavaScript calls UmlCanvas
JavaScript library to
render UML
Apex UML Canvas : Code Walkthrough
UmlCanvasController.java
 jQuery Ajax calls controller as user selects classes
1. /umlcanvas/{apexclass}/symboltable
Apex UML Canvas : Code Walkthrough
UmlCanvasController.java
1. /umlcanvas/{apexclass}/symboltable
2. /umlcanvas/{apexclass}/compile
3. /umlcanvas/containerasyncrequest/{id}
4. /umlcanvas/containerasyncrequest/{id}/{classname}/symboltable
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/symboltable
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java :
/containerasyncrequest/{id}
Apex UML Canvas : Code Walkthrough

UmlCanvasController.java :
/navigator/containerasyncrequest/{id}/{classname}/symboltable
Tooling API Other Features
 Debug Logs
 Execute Anonymous Apex Code
 Static Resources
 Inject Execution of Apex or SOQL Code for Debug
 Checkpoints to capture Heap Dumps
 Manage Custom Fields
 Accces Code Coverage Results
Other Uses of Tooling API
Ant Integration : Execute Apex Code from Ant!

Salesforce SE Execute an Apex class using Ant build script


Summary

Read Documentation closely!


 Force.com Tooling API Developer’s Guide
 Force.com Canvas Developer’s Guide
Symbol Table
 Has some gaps, still maturing
Spring MVC rocks!
 Great for those not familiar with Java Servlet API
 Shades of JavaScript Remoting
Andrew Fawcett
CTO,
@andyinthecloud

Você também pode gostar