Apex Developer Guide

Apex Developer Guide

Apex Developer Guide Version 53.0, Winter ’22 @salesforcedocs Last updated: August 27, 2021 © Copyright 2000–2021 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 Apex Developer Guide . 1 Getting Started with Apex . 1 Introducing Apex . 2 Apex Development Process . 11 Apex Quick Start . 17 Writing Apex . 23 Data Types and Variables . 23 Control Flow Statements . 50 Classes, Objects, and Interfaces . 58 Working with Data in Apex . 120 Running Apex . 225 Invoking Apex . 226 Apex Transactions and Governor Limits . 302 Using Salesforce Features with Apex . 315 Integration and Apex Utilities . 531 Debugging, Testing, and Deploying Apex . 597 Debugging Apex . 597 Testing Apex . 636 Deploying Apex . 670 Distributing Apex Using Managed Packages . 677 Apex Reference . 682 Appendices . 682 Shipping Invoice Example . 682 Reserved Keywords . 694 Documentation Typographical Conventions . 695 Glossary . 696 APEX DEVELOPER GUIDE Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Salesforce Platform server, in conjunction with calls to the API. This guide introduces you to the Apex development process and provides valuable information on learning, writing, deploying and testing Apex. For reference information on Apex classes, interfaces, exceptions and so on, see Apex Reference Guide. IN THIS SECTION: Getting Started with Apex Learn about the Apex development lifecycle. Follow a step-by-step tutorial to create an Apex class and trigger, and deploy them to a production organisation. Writing Apex Apex is like Java for Salesforce. It enables you to add and interact with data in the Lightning Platform persistence layer. It uses classes, data types, variables, and if-else statements. You can make it execute based on a condition, or have a block of code execute repeatedly. Running Apex You can access many features of the Salesforce user interface programmatically in Apex, and you can integrate with external SOAP and REST Web services. You can run Apex code using a variety of mechanisms. Apex code runs in atomic transactions. Debugging, Testing, and Deploying Apex Develop your Apex code in a sandbox and debug it with the Developer Console and debug logs. Unit-test your code, then distribute it to customers using packages. Apex Reference In Summer ’21 and later versions, Apex reference content is moved to a separate guide called the Apex Reference Guide. Appendices Glossary Getting Started with Apex Learn about the Apex development lifecycle. Follow a step-by-step tutorial to create an Apex class and trigger, and deploy them to a production organisation. IN THIS SECTION: Introducing Apex Apex code is the first multitenant, on-demand programming language for developers interested in building the next generation of business applications. Apex revolutionizes the way developers create on-demand applications. Apex Development Process In this chapter, you’ll learn about the Apex development lifecycle, and which organization and tools to use to develop Apex. You’ll also learn about testing and deploying Apex code. 1 Apex Developer Guide Introducing Apex Apex Quick Start This step-by-step tutorial shows how to create a simple Apex class and trigger, and how to deploy these components to a production organization. Introducing Apex Apex code is the first multitenant, on-demand programming language for developers interested in building the next generation of business applications. Apex revolutionizes the way developers create on-demand applications. While many customization options are available through the Salesforce user interface, such as the ability to define new fields, objects, workflow, and approval processes, developers can also use the SOAP API to issue data manipulation commands such as delete(), update() or upsert(), from client-side programs. These client-side programs, typically written in Java, JavaScript, .NET, or other programming languages, grant organizations more flexibility in their customizations. However, because the controlling logic for these client-side programs is not located on Salesforce servers, they are restricted by the performance costs of making multiple round-trips to the Salesforce site to accomplish common business transactions, and by the cost and complexity of hosting server code, such as Java or .NET, in a secure and robust environment. IN THIS SECTION: 1. What is Apex? Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects. 2. Understanding Apex Core Concepts Apex code typically contains many things that you might be familiar with from other programming languages. 3. When Should I Use Apex? The Salesforce prebuilt applications provide powerful CRM functionality. In addition, Salesforce provides the ability to customize the prebuilt applications to fit your organization. However, your organization may have complex business processes that are unsupported by the existing functionality. In this case, Lightning Platform provides various ways for advanced administrators and developers to build custom functionality. 4. How Does Apex Work? All Apex runs entirely on-demand on the Lightning Platform. Developers write and save Apex code to the platform, and end users trigger the execution of the Apex code via the user interface. 5. Developing Code in the Cloud The Apex programming language is saved and runs in the cloud—the multitenant platform. Apex is tailored for data access and data manipulation on the platform, and it enables you to add custom business logic to system events. While it provides many benefits for automating business processes on the platform, it is not a general purpose programming language. 2 Apex Developer Guide Introducing Apex What is Apex? Apex is a strongly typed, object-oriented programming language that allows developers to execute EDITIONS flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers Available in: Salesforce to add business logic to most system events, including button clicks, related record updates, and Classic (not available in all Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects. orgs) and Lightning Experience Available in: Enterprise, Performance, Unlimited, Developer, and Database.com Editions You can add Apex to most system events. As a language, Apex is: Integrated Apex provides built-in support for common Lightning Platform idioms, including: • Data manipulation language (DML) calls, such as INSERT, UPDATE, and DELETE, that include built-in DmlException handling 3 Apex Developer Guide Introducing Apex • Inline Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) queries that return lists of sObject records • Looping that allows for bulk processing of multiple records at a time • Locking syntax that prevents record update conflicts • Custom public API calls that can be built from stored Apex methods • Warnings and errors issued when a user tries to edit or delete a custom object or field that is referenced by Apex Easy to use Apex is based on familiar Java idioms, such as variable and expression syntax, block and conditional statement syntax, loop syntax, object and array notation. Where Apex introduces new elements, it uses syntax and semantics that are easy to understand and encourage efficient use of the Lightning Platform. Therefore, Apex produces code that is both succinct and easy to write. Data focused Apex is designed to thread together multiple query and DML statements into a single unit of work on the Salesforce server. Developers use database stored procedures to thread together multiple transaction statements on a database server in a similar way. Like other database stored procedures, Apex does not attempt to provide general support for rendering elements in the user interface. Rigorous Apex is a strongly typed language that uses direct references to schema objects such as object and field names. It fails quickly at compile time if any references are invalid. It stores all custom field, object, and class dependencies in metadata to ensure that they are not deleted while required by active Apex code. Hosted Apex is interpreted, executed, and controlled entirely by the Lightning Platform. Multitenant aware Like the rest of the Lightning Platform, Apex runs in a multitenant environment. So, the Apex runtime engine is designed to guard closely against runaway code, preventing it from monopolizing shared resources. Any code that violates limits fails with easy-to-understand error messages. Easy to test Apex provides built-in support for unit test creation and execution. It includes test results that indicate how much code is covered, and which parts of your code could be more efficient. Salesforce ensures that all custom Apex code works as expected by executing

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    716 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us