Apex

First Udemy Course

I can’t believe it’s been almost six months since my last post. What a weird year 2020 has been. Early this year I decided to create a Udemy course for beginning Apex developers. My goal was to create a course that would teach programming fundamentals and the Apex langauge at the same time. I wanted it to be approachable by students who are new to Apex, new to Salesforce or new to both.

Format DateTime Data in Apex

The DateTime object in Apex includes rich support for formatting dates using Java’s SimpleDateFormat. To format a DateTime, call the format() method and pass it a format string. The following table includes the most common elements you can include in the format string. All examples are using the date and time when I’m writing this — March 8, 2020 at 3:45pm. Letter Component Example y Year 2020 M Month in year 3 w Week in year 11 W Week in month 2 D Day in year 68 d Day in month 8 E Day name Sunday u Day of week (1 = Monday, 7 =Sunday) 7 a AM/PM PM H Hour (0 – 23) 15 K Hour (0 – 11) 3 m Minute 45 s Second 12 S Millisecond 71 z Time Zone PDT Repeat Letters to Modify the Presentation Many of the letters can be repeated to modify the presentation of the component they represent.

Display Toast Notifications for Server Side Events with Lightning Web Components

NOTE: The project discussed in this post is on GitHub. I recently needed to display a message to users in response to a server side event. Sometimes the message would only need to be displayed for the user who generated the event. For example, a user saves a record and something happens in a trigger they need to be notified of. We also had a use case where we want to notify every user who is currently using a particular application.

Converting Numbers to Text

Over the summer I worked on a real estate project. Some of the Salesforce data is used to generate documents and some of those documents need to have numbers spelled out. For example, 1,535,000 would be spelled out as one million five hundred thirty-five thousand. I didn’t like the solutions I found online so I spent a little time coding my own. It’s not a common use case but I wanted to post my solution in case others find it useful.

Unit Test a Salesforce REST API

This post is a continuation of the previous two posts where we created a Salesforce DX REST API project and then coded HTTP methods to perform CRUD operations. The full project is on GitHub. In this post we will cover some general unit testing concepts and specifically how to unit test HTTP methods in Apex. Arrange, Act, Assert There is a common pattern in unit tests called Arrange, Act, Assert. It states that each unit test does these three things.

Create a REST API with SFDX, Part 2

In part 1 we setup our scratch org and created our custom object. In this post we will build out the API. First, create a new class in Visual Studio Code called HouseService. From the command palette in VS Code select SFDX: Create Apex Class and enter the name of the class. A few things to note about the class definition: @RestResource identifies our class as a REST endpoint.

Create a REST API with SFDX, Part 1

In the next couple of posts we will create a REST API with Salesforce DX. First we’ll create a custom object and then expose that object through a REST API. When it’s complete we will have an API for a custom object that is similar to the built in Salesforce REST API for sObjects. You can download the code for this project from GitHub. In this first post we’re going to take care of the preliminaries: