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

  1. 368 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Mastering Apex Programming

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

Paul Battisson

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Mastering Apex Programming est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Mastering Apex Programming par Paul Battisson en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Negocios y empresa et Inteligencia empresarial. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2020
ISBN
9781800204331

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 des matiĂšres