Reader small image

You're reading from  Salesforce Data Architect Certification Guide

Product typeBook
Published inNov 2022
PublisherPackt
ISBN-139781801813556
Edition1st Edition
Right arrow
Author (1)
Aaron Allport
Aaron Allport
author image
Aaron Allport

Aaron Allport is a Chief Technical Officer, and has worked with CRM systems and integrations for his entire professional career. Aaron specializes in Salesforce technical architecture and integration, helping his clients ensure they get the most from their technology investment. Aaron has spoken at Dreamforce, written about everything from DevOps to Data Architecture online, and can regularly be found at the Salesforce London Developer Meetup.
Read more about Aaron Allport

Right arrow

Data APIs and Apex

In this chapter, we’re going to explore the various Salesforce Application Programming Interfaces (APIs) that relate to data operations. Understanding the various APIs and Apex operations is important in rounding out your knowledge of the programmatical capabilities of the Salesforce platform as it relates to data operations. When taking the exam, you may get quizzed on hypothetical scenario questions that involve answering based on use of a Salesforce API or the use of Apex code to achieve a certain data operation. Therefore, this is crucial in rounding out your knowledge as a Salesforce Data Architect. In this chapter, we’re also going to look at Apex facilities relating to data, including database operations, working with batch data methods, and asynchronous Apex.

In this chapter, we will cover the following topics:

  • How to call an API
  • Lightning Platform Data APIs
  • Apex Database operations
  • Batch Apex
  • Asynchronous Apex
  • ...

How to call an API

While the design and implementation of APIs is out of the scope of this book, we’ll briefly cover how to call an API, namely the Salesforce REST API. At a high-level, an API is essentially a request to an endpoint (a URL) with an XML or JSON body (known as the request payload). The endpoint will return a response payload, which may be XML or JSON depending on how it is configured.

Let’s cover what calling a REST API looks like, and examine the request and response payloads as we do. For this, we’re going to use an online utility called Workbench, which is a free-to-use web application for interacting with your Salesforce instance. Note that it is not an official Salesforce product, and therefore support is limited.

To begin, open a web browser and navigate to the URL https://workbench.developerforce.com/login.php

Next, click the checkbox to agree to the terms and conditions, and click the Login with Salesforce button:

...

Lightning Platform Data APIs

There are 11 APIs available on the Salesforce Lightning Platform that cover all sorts of operations, including working with metadata (the Metadata API), building user interfaces that let users interact with Salesforce records (the User Interface API), and for use when building custom development modules or applications (the Tooling API).

I appreciate that not everyone reading this book will be a software engineer and therefore understand the intricacies of how APIs work. However, understanding how Salesforce programmatically exposes data-related operations is necessary. This will give you a more complete understanding of how data can be affected by operations on the Salesforce platform outside of general user interface interaction.

In a nutshell, an API provides a means by which two software applications can talk to each other. Typically, one of the applications exposes a select set of operations that other applications can invoke. APIs also support...

Apex Database operations

In Apex code, it is possible to interact with the Salesforce database in one of two ways: as Database Manipulation Language (DML) statements or using Apex Database class methods. DML statements take the form of insert, update, upsert, or delete operations. Take the following example of a DML insert statement:

public class dmlStatementTest {
    public void testStatement() {
        List<Account> accounts;
        accounts.add(new Account(Name='Packt UK'));
        accounts.add(new Account(Name='Packt India'));
        insert accounts;
    }
}

Database Apex methods work slightly differently because rather than using a keyword such as insert or update, you call a method of the Database class instead. I’d...

Batch Apex

As its name suggests, batch Apex is used to invoke and process a potentially large number of records in batches, where the number of records is determined by issuing a query to Salesforce. This execution typically lends itself well to situations where a potentially large number of records could need processing, yet the exact number of records isn’t known, such as updating records as part of an overnight batch procedure.

batch Apex classes must implement the Database.Batchable interface and therefore the start, execute, and finish methods. A bare-bones Apex class for implementing the Database.Batchable interface would look similar to the following:

public class myBatchClass implements Database.Batchable {
   public Database.QueryLocator start(Database.BatchableContext info){}
   public void execute(Database.BatchableContext info, List<sObject> scope){}
   public void finish(Database.BatchableContext...

Asynchronous Apex

Apex code can be run in an asynchronous manner using batch classes, and subsequently queued or scheduled. Apex code can also be written to run in the background when it’s asynchronous by using the @future annotation on methods. All Apex web service callouts are @future methods, with a callout=true parameter, in that they are forced to run asynchronously, and in the background, user interface operations are not held up by their invocation.

Another way to think about asynchronous Apex is that it essentially executes in its own time. A bonus of using @future methods is that the governor limits are higher, meaning SOQL query size and heap size limits are more generous as the execution is in its own time, on its own system thread.

When writing Apex methods that are annotated using @future, they must be static and only return a void return type – that is, return nothing.

Here’s an example of a bare-bones asynchronous Apex class:

public class...

Summary

In this chapter, we explored the more programmatic elements of the Salesforce platform when it comes to manipulating data. We looked at the REST and SOAP APIs before looking at the Bulk API. Then, we turned our attention to Apex Database class operations, batch Apex, and asynchronous Apex using @future annotations. With these concepts understood, you, as a Salesforce Data Architect, are now well versed to make decisions on programmatic behavior affecting the state of a solution.

In the next chapter, we’ll explore more of the concepts around how you can mitigate Large Data Volume (LDV) concerns through practical examples and how to load massive amounts of data.

Practice questions

Test your knowledge of the topics covered in this chapter by answering the following questions:

  1. What is the only data format that’s suitable for SOAP API requests?
  2. What are the two job types that the Bulk API is used for?
  3. What type of resource is used to describe the operations that are offered by a SOAP API that can be imported by calling applications?
  4. How many retries are attempted by a Bulk API Query operation?
  5. What batch size are records divided into when running a Bulk API Ingest operation?
  6. What operations are supported as part of a Bulk API Ingest job?
  7. What interface is extended when writing an Apex batch class?
  8. What annotation is used for asynchronous Apex methods?
  9. The Apex insert statement, followed by a list of records of a particular object type, is an example of what type of approach when working with the Salesforce database?
  10. The Apex Database class’s insert/update/upsert/delete methods have a Boolean...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Salesforce Data Architect Certification Guide
Published in: Nov 2022Publisher: PacktISBN-13: 9781801813556
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Aaron Allport

Aaron Allport is a Chief Technical Officer, and has worked with CRM systems and integrations for his entire professional career. Aaron specializes in Salesforce technical architecture and integration, helping his clients ensure they get the most from their technology investment. Aaron has spoken at Dreamforce, written about everything from DevOps to Data Architecture online, and can regularly be found at the Salesforce London Developer Meetup.
Read more about Aaron Allport