Mastering Apex Programming
eBook - ePub

Mastering Apex Programming

A developer's guide to learning advanced techniques and best practices for building robust Salesforce applications

Paul Battisson

Share book
  1. 368 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Apex Programming

A developer's guide to learning advanced techniques and best practices for building robust Salesforce applications

Paul Battisson

Book details
Book preview
Table of contents
Citations

About This Book

Get to grips with the advanced features of Apex programming for Salesforce developers using this comprehensive guide

Key Features

  • Discover how to build reliable applications with Apex by avoiding common mistakes and pitfalls
  • Learn how to use the different asynchronous programming tools in Apex
  • Profile and improve the performance of your Apex code

Book Description

As applications built on the Salesforce platform are now a key part of many organizations, developers are shifting focus to Apex, Salesforce's proprietary programming language. As a Salesforce developer, it is important to understand the range of tools at your disposal, how and when to use them, and best practices for working with Apex. Mastering Apex Programming will help you explore the advanced features of Apex programming and guide you in delivering robust solutions that scale.

This book starts by taking you through common Apex mistakes, debugging, exception handling, and testing. You'll then discover different asynchronous Apex programming options and develop custom Apex REST web services. The book shows you how to define and utilize Batch Apex, Queueable Apex, and Scheduled Apex using common scenarios before teaching you how to define, publish, and consume platform events and RESTful endpoints with Apex. Finally, you'll learn how to profile and improve the performance of your Apex application, including architecture trade-offs.

With code examples used to facilitate discussion throughout, by the end of the book, you'll have developed the skills needed to build robust and scalable applications in Apex.

What you will learn

  • Understand common coding mistakes in Apex and how to avoid them using best practices
  • Find out how to debug a Salesforce Apex application effectively
  • Explore different asynchronous Apex options and their common use cases
  • Discover tips to work effectively with platform events
  • Develop custom Apex REST services to allow inbound integrations
  • Build complex logic and processes on the Salesforce platform

Who this book is for

This book is for Salesforce developers who are interested in mastering Apex programming skills. You'll also find this book helpful if you're an experienced Java or C# developer looking to switch to Apex programming for developing apps on the Salesforce platform. Basic Apex programming knowledge is essential to understand the concepts covered.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Apex Programming an online PDF/ePUB?
Yes, you can access Mastering Apex Programming by Paul Battisson in PDF and/or ePUB format, as well as other popular books in Commerce & Business Intelligence. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781800204331
Edition
1

Section 1 – Triggers, Testing, and Security

In this section, we will begin by reviewing common mistakes made by developers in their Apex code and how to avoid them. This will lead us into a discussion around debugging Apex when we find an error. Following this, we will discuss how to work with Salesforce triggers effectively and how to improve our Apex testing, as well as understand how to improve security within our code.
This section covers the following chapters:
  • Chapter 1, Common Apex Mistakes
  • Chapter 2, Debugging Apex
  • Chapter 3, Triggers and Managing Trigger Execution
  • Chapter 4, Exceptions and Exception Handling
  • Chapter 5, Testing Apex Code
  • Chapter 6, Secure Apex Programming

Chapter 1: Common Apex Mistakes

In this chapter, we will cover common mistakes made when developing in Apex and the appropriate defensive programming techniques to avoid them. Before we begin to move into more advanced topics within the Apex language, it is important that we first of all ensure a common grounding in removing any common errors within our code. For some of you, this material may simply be a refresher or a reiteration of some practices; however, these mistakes form the basis of many of the common exceptions and errors that I have seen in my time working with the Salesforce platform. By the end of this chapter, you will hopefully be more aware of when these mistakes may occur within your code and be able to develop in a defensive style to avoid them.
In this chapter, we will cover the following topics:
  • Null pointer exceptions
  • Bulkification of Apex code to avoid Governor Limits
  • Hardcoding references to specific object instances
  • Patterns to deal with managing data across a transaction

Technical Requirements

The code for the chapter can be found here, https://github.com/PacktPublishing/Mastering-Apex-Programming/tree/Chapter-1.

Null pointer exceptions

Almost every developer working with the Salesforce platform has encountered the dreaded phrase Attempt to de-reference a null object. At its heart, this is one of the simplest errors to both generate and handle effectively, but its error message can cause great confusion for new and experienced developers alike, as it is often unclear how the exception is occurring.
Let's start by discussing in the abstract form how the error is generated. This is definitively a runtime error, caused by the system attempting to read data from memory where the memory is blank. Apex is built on top of the Java language and uses the Java Virtual Machine (JVM) runtime under the hood. What follows is a highly simplified discussion of how Java manages memory, which will help us to understand what is happening under the hood.
Whenever an object is instantiated in Java, it is created and managed on the heap, which is a block of memory used to dynamically hold data for objects and classes at runtime. A separate set of memory, called the stack, stores references to these objects and instances. So, in simplistic terms, when you instantiate an instance called paul of a Person class, that instance is stored on the heap and a reference to this heap memory is stored on the stack, with the label paul. Apex is built on Java and compiles down to Java bytecode (this started after an update from Salesforce in 2012), and, although Apex does not utilize a full version of the JVM, it uses the JVM as the basis for its operations, including garbage and memory management.
With this in mind, we are now better able to understand how the two most common types of NullPointerException within Apex occur: when working with specific object instances and when referencing values on maps.

Exceptions on object instances

Let's imagine I have the following code within my environment:
public class Person {
public String name;
}
Person paul;
In this code, we have a Person class defined, with a single publicly accessible member variable. We have then declared a variable, paul, using this new data type. In memory, Salesforce now has a label on the stack called paul that is not pointing to any address on the heap, as paul currently has the value of null.
If I now attempt to run System.debug(paul.name);, we will get a NullPointerException exception with the message Attempt to de-reference a null object. What is happening is that the system is trying to use the paul variable to retrieve the object instance and then access the name property of that instance. Because the instance is null, the reference to this memory does not exist, and so NullPointerException is thrown; that is, we have nothing to point with.
With this understanding of how memory management is working under the hood (in an approximate fashion) and how we are generating these errors, it is therefore pretty easy to see how we code against them—avoid calling methods and accessing variables and properties on an object that has not been instantiated. This can be done by ensuring we always call a constructor when initializing a variable, as shown in the following code snippet:
Person paul = new Person();
In general, when developing, we should pay atte...

Table of contents