Enhance Salesforce with Code Salesforce, Summer ’16 @salesforcedocs Last updated: August 17, 2016 © Copyright 2000–2016 salesforce.com, inc. All rights reserved. Salesforce is a registered trademark of salesforce.com, inc., as are other names and marks. Other marks appearing herein may be trademarks of their respective owners. CONTENTS ENHANCE SALESFORCE WITH CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Welcome, Salesforce Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Salesforce Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Deploy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 ENHANCE SALESFORCE WITH CODE Welcome, Salesforce Developers This documentation provides information about enhancing your Salesforce organization by developing custom applications and integrating your external applications. This documentation is organized by task so you can quickly find the information you need: • Writing Code—Write code using the Apex programming language to add business logic or use the Visualforce markup language to create the user interface. In addition, you’ll find information about integrating your application using APIs and authenticating your external applications. • Debugging Your Code—Debug your application using the Developer Console. • Testing Your Changes—Test your Apex code and work with the test tools. • Deploy—Deploy your changes to another organization using change sets and other tools. For the complete set of developer documentation, see https://developer.salesforce.com/page/Documentation. Salesforce Development Tools This table summarizes the functionality of the various Salesforce development tools. EDITIONS Tool Code Debug Test Deploy Available From Available in: Salesforce Classic Force.com Developer Console Your Name or the quick access menu The available tools vary according to which ( ) Salesforce Edition you have. Force.com IDE developer.salesforce.com Visualforce development mode Setup or your personal footer settings Code editor Setup Apex Test Execution Setup Change Sets Setup Force.com Migration Tool Setup 1 Enhance Salesforce with Code Code Note: The Force.com IDE is a free resource provided by Salesforce to support its users and partners, but is not considered part of our Services for purposes of the Salesforce Master Subscription Agreement. SEE ALSO: DeveloperForce Tools Page Developer Console Functionality Enabling Development Mode Code Writing Code This section contains information about writing code to extend your organization. • Developer Console Functionality • Securing Your Code • Developer Console Query Editor • Working with Code SEE ALSO: Debugging Your Code Testing Your Changes Deploy Developer Console The Developer Console is an integrated development environment with a collection of tools you can use to create, debug, and test applications in your Salesforce org. IN THIS SECTION: Open the Developer Console It takes only a couple of clicks to open the Developer Console from Salesforce Classic or Lightning Experience. The Developer Console is an integrated development environment with a collection of tools you can use to create, debug, and test applications in your Salesforce org. Developer Console Functionality The Developer Console can help with many of your development tasks. Developer Console User Interface Overview The Developer Console includes a collection of useful tools for coding, debugging, and testing applications. Developer Console Command Line Reference The Developer Console includes a command line for various useful commands. To open or close the Developer ConsoleCommand Line Window, press CTRL+SHIFT+L. 2 Enhance Salesforce with Code Developer Console Developer Console Workspaces A workspace is a collection of resources represented by tabs in the main panel of the Developer Console. Open the Developer Console USER PERMISSIONS EDITIONS To use the Developer Console: “View All Data” Available in: Salesforce Classic and Lightning To execute anonymous Apex: “Author Apex” Experience To use code search and run SOQL or SOSL “API Enabled” Available in: Enterprise, on the query tab: Performance, Unlimited, To save changes to Apex classes and “Author Apex” Developer, and triggers: Database.com Editions To save changes to Visualforce pages and “Customize Application” components: To save changes to Lightning resources: “Customize Application” It takes only a couple of clicks to open the Developer Console from Salesforce Classic or Lightning Experience. The Developer Console is an integrated development environment with a collection of tools you can use to create, debug, and test applications in your Salesforce org. To open the Developer Console from Salesforce Classic: 1. Click Your Name. 2. Click Developer Console. To open the Developer Console from Lightning Experience: 1. Click the quick access menu ( ). 2. Click Developer Console. Developer Console Functionality USER PERMISSIONS EDITIONS To use the Developer Console: “View All Data” Available in: Salesforce Classic and Lightning To execute anonymous Apex: “Author Apex” Experience To use code search and run SOQL or SOSL “API Enabled” Available in: Enterprise, on the query tab: Performance, Unlimited, To save changes to Apex classes and “Author Apex” Developer, and triggers: Database.com Editions To save changes to Visualforce pages and “Customize Application” components: To save changes to Lightning resources: “Customize Application” 3 Enhance Salesforce with Code Developer Console The Developer Console can help with many of your development tasks. How Do You Use the Developer Console? Debugging and Troubleshooting The Developer Console provides a convenient set of tools for efficiently tracking down logical issues. • View Logs: Use the Logs tab to view a list of logs. Open logs in the Log Inspector. Log Inspector is a context-sensitive execution viewer that shows the source of an operation, what triggered the operation, and what occurred afterward. Use this tool to inspect debug logs that include database events, Apex processing, workflow, and validation logic. • Set and View Checkpoints in Apex Code: Use the Developer Console to set checkpoints to identify the source of errors. For example, if you want to understand why a certain request generates an error, you can review the execution, identify the offending logic, and set a checkpoint. When you execute the process again, you can inspect the request at that specific point to understand in detail how to improve your code. While the Developer Console can't pause execution like a traditional debugger, it provides cloud developers much of the same visibility, and reduces the need to instrument code with System.debug commands. Editing and Navigating Source Code The Developer Console allows you to browse, open, edit and create source code files. • Browse Packages in Your Organization: Navigate the contents of packages created in your organization. • View and Edit Apex Classes and Triggers: Open and edit Apex triggers and classes, and open a read-only view of your custom object definitions. • View and Edit Lightning Components: Open and edit Lightning resources, such as an application, component, event, or interface. • View and Edit Visualforce Pages and Components: Open and edit Visualforce pages and components. • Use the Source Code Editor: Open a working set of code files and switch between them with a single click. The Developer Console Source Code Editor includes an auto-complete feature for Apex code. Testing and Validating Performance The Developer Console has a number of features dedicated to testing code and analyzing performance. • Test Apex Code: Use the Developer Console to check code coverage and run Apex tests, including unit tests, functional tests, regression tests, and so on. To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword or the isTest annotation in the method definition. Also, test methods must be defined in test classes, that is, classes annotated with isTest. • Inspect Logs for Performance Issues: Log Inspector is a context-sensitive execution viewer that shows the source of an operation, what triggered the operation, and what occurred afterward. Use this tool to inspect debug logs that include database events, Apex processing, workflow, and validation logic. Open a debug log and view the aggregated performance of an operation in the Performance Tree. The Executed Units panel breaks up the request both by time and type, and categorizes the timings by methods, queries, workflows, callouts, DML, validations, triggers, and pages, giving you a clear idea of where to find performance issues. Use the Timeline panel to see a timeline view of the overall request and walk through the events for a given block. The Limits panel provides a summary view of resources used and maps them against your allocated request limits. Executing SOQL and SOSL Queries The Developer Console provides a simple interface for managing SOQL and SOSL queries. • Edit and Execute SOQL and SOSL Queries: Use the Query Editor to query data from your organization. • View Query Results: Results are displayed in a Query Results grid, in which you can open, create, update, and delete records. For SOSL search results with multiple objects, each object is displayed on a separate tab. 4 Enhance Salesforce with Code Developer Console Developer Console User Interface Overview The Developer Console includes a collection of useful tools for coding, debugging, and testing applications. The Developer Console is organized into the following sections: 1. Menubar 2. Workspace with a tab for each open item 3. Logs, Tests, and Problems panel Menubar The menubar includes the following drop-down menus: • The File menu allows you to open and create resources. • The Edit menu allows you to search and edit your code files. • The Debug menu provides access to a range of tools and settings. • The Test menu provides access to testing tools. • The Workspace menu allows you to choose and manage workspaces. • The Help menu includes links to the online help, a reference page of shortcut keys, and the Developer Console preferences page. Workspace A workspace is a collection of resources represented by tabs in the main panel of the Developer Console. 5 Enhance Salesforce with Code Developer Console The detail view or editor shown in each tab is determined by the type of resource open in the tab. For example, source code opens in the Source Code Editor, logs open in the Log Inspector, and so on. You can create a workspace for any group of resources that you use together to keep your work organized. For example, you can create one workspace for source code and another for debug logs, switching between them as you code and test. See Developer Console Workspaces. Logs, Tests, and Problems Panel The lower panel in the Developer Console includes a collection of useful tabs: • The Logs tab displays available logs. • The Tests tab displays available tests. • The Checkpoints tab displays available checkpoints. • The Query Editor tab allows you to execute a SOQL or SOSL query on the data in your organization. • The View State tab, if enabled, allows you to examine the view state of a Visualforce page. • The Progress tab displays all asynchronous requests in real time. To see only the operations that are in progress, select Hide Finished Runs. To terminate any deployments that haven’t finished, click Cancel All Deployments. When you terminate a deployment, a residual polling thread appears in the Progress tab with a short delay. Partial deployments are not possible. To clear the polling task immediately, refresh the Developer Console. • The Problems tab shows the details of compilation errors in the Source Code Editor. Changes you make are compiled and validated in the background. While you’re editing code, an error indicator displays beside lines that contain errors. Click a row in the Problems tab to jump to the line of code that caused the error. Note: After twenty minutes of inactivity, the Developer Console stops polling for new logs, test runs, and checkpoints. To resume updates, click Debug > Resume Updating. Keyboard shortcuts To see a list of Developer Console keyboard shortcuts, click Help > Shortcut Keys or press CTRL+SHIFT+?. Developer Console File Menu The Developer ConsoleFile menu allows you to manage your Apex triggers and classes, Visualforce pages and components, and static resources (text, XML, JavaScript, or CSS). It includes these options. • New: Creates a resource and opens it in the Source Code Editor. You can create these resources: – Apex class or trigger; To create an Apex trigger, first select the object to associate with the trigger. – Lightning application, component, event, interface, or tokens bundle; For more information, see Lightning Component Framework Overview on page 76. – Visualforce page or component – Static resource file (text, XML, JavaScript, or CSS) • Open: Launches a File Open window that allows you to browse and open your application code and data objects. • Open Resource: Launches an Open Resource window that allows you to search for files by name. • Open Lightning Resources: Launches an Open Lightning Resources window that allows you to search for Lightning components resources by name. • Open Log: Opens the selected log in the Log Inspector. You can also access logs from the Logs tab. • Open Raw Log: Opens the selected log, in plain text. 6
Description: