Write Business Logic in Apex
Follow Along with Trail Together
Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.
(This clip starts at the 34:00 minute mark, in case you want to rewind and watch the beginning of the step again.)
Introduction
Apex is the strongly typed, object-oriented programming language that's optimized to run in the Salesforce multitenant architecture. Apex allows developers to automate complex backend business processes, and gets compiled into Java bytecode.
The Apex language is optimized to interact with Salesforce data and is tightly integrated with the Salesforce persistence layer. Apex provides SOQL (Salesforce Object Query Language), similar to SQL, for executing queries and DML (Data Manipulation Language) statements for performing database operations with the objects you created earlier.
Create and Deploy the Apex Class
- In Visual Studio Code, under force-app/main/default, right-click classes and select SFDX: Create Apex Class.
- In the Visual Studio Code Command Palette, name the class
HouseService
. Click Enter | Enter.
- Replace the contents of the file with the following code.
public with sharing class HouseService { @AuraEnabled(cacheable=true) public static List<House__c> getRecords() { try { // Create a list of House records from a SOQL query List<House__c> lstHouses = [ SELECT Id, Name, Address__c, State__c, City__c, Zip__c FROM House__c WITH USER_MODE ORDER BY CreatedDate LIMIT 10 ]; return lstHouses; } // Code to handle exception catch (Exception e) { throw new AuraHandledException(e.getMessage()); } } }
Code Highlights In this code, you create a class HouseService that has a method named getRecords. The method returns the list of house records (as an ordered collection) by querying the House__c object.
Notice that in the HouseService Apex class, you automatically surface the object (House__c) you created previously (in Step 2) as a class without writing a class file. Additionally, you can reference the fields in the House__c object as properties without having to declare them.
We've used SOQL to write our queries referencing the object and the fields on the object. The query compiles and thus is type-checked at compile time.
You have established a tight coupling between the data model and the Apex class. Now you won't be able to break the code because it will not compile if you make a code change inconsistent with the data model.
- Save this file.
- Right-click HouseService.cls and select SFDX: Deploy Source to Org. You see a confirmation message that the Apex class successfully deployed to the org. Deploying your code also compiles it on the server.
Now, test whether this class returns query results as expected using an anonymous script. An anonymous script is Apex code that doesn't get stored in the metadata but that can be compiled and executed.
Follow the steps below to create an anonymous script to test.
- Create a new file named dreamhouseapp.apex in the scripts/apex folder.
- Replace the contents of the file with the following code:
System.debug(HouseService.getRecords());
- Click Code Lens Execute Anonymous Apex highlighted in pink in the below screenshot.
If the Apex class you've written is functional, you see the query results in the output panel shown below.
Apex is opinionated, tightly coupled, and optimized to work with business apps that work well with Salesforce. It fails quickly at compile time if any references are invalid.
Because it integrates with the Salesforce persistence layer and has built-in support to work with Salesforce data directly without having to wire additional data layers, you can be more productive. And on top of that, Apex automatically encourages good coding practices by enforcing governor limits and testing requirements.
Resources
- GitHub: Apex-Recipes
- Trailhead: Build Apex Coding Skills
- Trailhead: Apex Triggers
- Salesforce Developer Documentation: Apex Replay Debugger