About Brooks Johnson

Retired soldier and self-taught Salesforce developer.

Getting Started With Apex

I have been meaning to share create some coding videos for quite a while now. This is the first in what I hope will be a series. I wanted to share how I work through some the Trailhead challenges that seemed impossible to me a couple of years ago.

I still remember what gave me fits and I hope that I can explain those things to others that are just getting started. Let me know what you might like to see next.


Understanding Recursion In Apex

I think that a significant number of people in the Salesforce ecosystem first encounter the idea of recursion when they are studying for the Platform Developer 1 exam. This was the case for me. And, if you don’t have a computer science background it’s unlikely you would have encountered the concept in the past. My own experience was that this created a very limited understanding of recursion. I only understood it as it applied to unwanted trigger re-entry or an endless loop. That is to say that I thought ‘recursion = bad’. But the reality is that it is powerful and fundamental concept in computer science. And even those us of that work with languages that are not really friendly to recursion, like Apex, should still understand it.

What is Recusion and How Do We Use It?

A recursive method is nothing more than a method that calls itself. Anytime a method is called it goes on the top of something known as the call stack. Methods are executed from the top of the calls stack to the bottom. In the case of Apex we have limit to our stack depth of 1000. That means if you have a recursive method it can never call itself more than 1000 times. This is useful in preventing a endless loop and ensuring the fair use of resources when we are dealing with the multi-tenant architecture of the Salesforce cloud. Here is great article from Free Code Camp detailing the call stack and recursion.

Let’s look at a simple of example of a method in Apex that calculates a factorial. This is probably the classic intro to recursive code. If you are rusty on your high school math a factorial is the product of an integer and all the integers below it. The factorial of 4 or 4! = 4 * 3 * 2 *1 or 24. The code below is an implementation of a recursive factorial calculator in Apex.

The condition in the If statement is what is known as our base case. This is what is telling the method exit from the recursive calls. Without it we will create an endless loop. If we pass in the number 3 the method would execute as follows: 3 is greater than 0 so the method calls itself with a return value of 3 multiplied by the the return value of our method but now the variable num = 2. The method calls keep getting placed on the call stack until the value of num = 0. When that happens we exit out of our recursive loop and return the value of 1. Now all the previous method calls that we were waiting on the call stack get popped off and execute in reverse order. We multiply 1 times 2 and then 2 times 3 for the result of 6.

If we take a look at our debug log we can see the recursive method in action and that the correct result was returned.

Note that if we try it with a larger number we can see that we will hit the Apex stack depth governor limits.

Below is an example of an iterative factorial calculator -this is what we would implement in Apex unless we could be certain we will always be dealing with fairly small numbers. Now, rather than the method calling itself we are using a standard for loop and iterating over the number. The iterativeFactorialCalc method will only go on the call stack one time and will remain in the loop until the variable i is greater than or equal to the number being evaluated.

The use cases for recursion in Apex development are limited. But, recursion itself is not an inherent evil and is commonly used in applications with languages that support it. While it will never be a a go to technique for a Salesforce developer or architect, I think it is a critical concept to understand.

Feel free to get the code for this post here.

Classic CS Algorithms In Apex

The Salesforce ecosystem always seems to have a higher number of self taught developers than other industries. I, certainly, count myself among them. It was not too many years ago that I could not have told you the difference between Java and HTML. As my own developer career has grown I have realized that two of the common concerns with self taught or boot camp grads is their lack of understanding of core computer science principles. Particularly, algorithms and data structures and how to evaluate them. I know I wrote more than my share of nested for loops before I realized that they didn’t scale too well and have a way of blowing up your CPU governor limits at the worst time.

In my own studies I realized that there is a lack of examples of some of these classic CS concepts implemented in Apex. I wanted to give back by creating a GitHub repository of classic sort and search algorithms and blogging about some of them.

Bubble Sort In Apex

Bubble sort seems to be one of the first sorting algorithms that is taught. Typically as an example of of algorithm that scales poorly. The nested for loop means that sorting 10 items will take 100 operations, 100 items will take 1000 operations and so on. If you are starting your journey as a developer it is worth taking some time to understand time complexity and Big-O notation.

Lets talk about the code. If we look at the class BubbleSort in the image above we we can see it has two methods Sort and Swap. The Sort method accepts a list of integers and then create a basic for loop. We set the index of the loop to the last value in our collection. This is our unsorted partition of our collection. The loop will continue as long as the value of the unsortedIndex variable is greater than 0 and will decrement by 1 each time go through the loop. A list of 10 integers would result create an unsortedIndex of 9.

We then set up an inner for loop. The index starts at 0 and will loop as long as the value is less than that of the unsortedIndex in the parent loop. (This is a slightly optimized bubble sort) The inner loop then compares each value in the array. If the element at position i is greater than the element at position i+1 we call our Swap method and change their place in the array. This results in the largest element in the loop “bubbling” up to the end of the collection.

The image below is an example of bubble sort being called on a simple 5 integer list. On the right you can see the debug log to help you follow the logic of the algorithm. We can see that in our first traversal through the inner loop that 78 is greater than 4 and their positions are swapped. 78 is greater than 22 and so forth until 78 bubbles up to the end of the array. We now know that the largest value is in the last position of our array. We can reduce the size of our unsorted partition by 1 and and examine the remaining 4 values.

I hope that this is a useful post to new developers and illustrates the issues with nested for loops and large data volumes.

Live Development for Lightning

One of the things that always frustrated me about developing with Aura and Lightning Web Components was how long it took to preview even the smallest change. You want to update a background color? increase the font size by 1px? Then you need to push code to the scratch org and refresh the page and wait to see the changes, rinse and repeat. Definitely a tedious process, particularly when you are trying to figure why a component is throwing an error. This all changed in Winter 20 and now there is one more reason you should be adopting SFDX development tools into your workflow.

One of best, and I feel least appreciated features in Winter 20, was the beta release of local development for Lightning and Lightning Web Components. You can read more here. For the first time working on the Salesforce front end feels l any other kind of web development. Make a change and just preview it in the browser. You only need two commands from the SFDX CLI to make it happen. Install the plugin with sfdx plugins:install @salesforce/lwc-dev-server. And launch the local server with sfdx force:lightning:lwc:start. If it works you will see a message like this in your terminal. -This assumes you have the SFDX CLI tool installed to begin with. If not then go here.

Click the link to the local host , that appears in the terminal, and you will have a browser window that will look something like the image below. This is component that I have in development to manage time estimations for project management. Anyone with the PMP will be very familiar with the PERT estimate. If I make a change to the code in my my IDE no more pushing to a scratch org my local host page refreshes automatically and I can see my changes.

I hope everyone finds this feature as useful as I do.

Where To Begin

You signed up for Vetforce and Trailhead and now you are not sure what to do next. You are not alone, the list of career options and paths are a lot to take in: developer, admin, marketing, multiple types of consultant and business analysts. The most common question I get is “which one do I choose?” I can say, without hesitation, that you should start the admin trail, regardless of what your ultimate goals might be.

The admin certification is the foundation of of your Salesforce knowledge and career no matter if your eventual goal is to become a Advanced Developer or Certified Technical Architect. The admin certification is also a requirement for any of the consultant level certifications. If you are using Vetforce, I believe the decision is made for you and you must earn the admin certification before the developer path is open to you.

One of the keys to success on the Salesforce platform is how extendable it is without writing code. If you are a developer new to Salesforce this means that non-developers can impact your software. Even if you have a strong background in object oriented programing you will want to understand how a junior admin can break your code with a poorly tested validation rule.

The multileveled Salesforce security model is something else you have to understand to work effectively on the platform. If you are designing or implementing solutions you must understand how to use, roles, profiles, permission sets and public groups.

Salesforce Visual Workflow

Perhaps the most important reason for a developer to understand the declarative (clicks rather than code) side of the platform is determine when not to code. Is the only thing needed a simple field update? No problem -work flow rules or a process builder to the rescue. Do you need to work with arrays and create and delete records? Maybe a flow is an option.

Understanding when a to use a formula field or a roll up summary vs writing a a new class and method will speed your development process, allowing you to get your product to your users or faster and minimize the maintenance costs that come with programmatic solutions. If a user needs to the understand how discounting effects their margin a simple formula field will get you there much faster than coding a new method to do the same thing. Below is an example of this business case implemented in a formula and in code. And remember the programmatic solution still needs a trigger or another class to call it and unit tests. In Salesforce unit tests are a requirement. You can’t deploy code into production without them. No code coverage? No Deployment.

No matter where your career on the Salesforce platform takes you understanding how to be an Awesome Admin is the first step.

Success! You're on the list.

I’m not really interested in a career in Sales…

Or – What the heck is Salesforce anyway?

I know this supposed be a blog about coding but sometimes you need to begin at the beginning. It is my completely untested hypothesis that some vets / career changers never explore opportunities on the Salesforce platform because the name of the company is Salesforce. Sometimes I wish it was called Cloud-A-Tron or something. My wife still doesn’t really understand what I do: “something with computers”.

Salesforce is simply a tech company like IBM, or Google, Apple or Microsoft. Here is what The Street has to say.

Salesforce is a cloud computing service as a software (SaaS) company that specializes in customer relationship management (CRM). Salesforce’s services allow businesses to use cloud technology to better connect with customers, partners and potential customers. The software has become the number one for customer success and helps businesses track customer activity, market to customers and many more services. 

Salesforce impressed investors recently by crushing third quarter estimates, reporting a third quarter revenue of $3.39 billion – up 26% for the year. 

The software company has become very popular in recent years. TheStreet’s founder Jim Cramer even dubs the service as a ‘Cloud King’ and has been very bullish on the stock – seemingly with good reason.

Founded in 1999 by a former Oracle (ORCL – Get Report) executive Marc Benioff, Dave Moellenhoff, Frank Dominguez and Parker Harris, Salesforce is one of the first global companies to successfully employ a cloud-based CRM software. Salesforce has been able to leverage cloud technology and build a variety of applications for businesses to better connect to their customers and help give them key insights into their services through analytics and apps.


Pretty cool, right? At some level all business is sales. But a career in Salesforce is an IT career focussing on cloud computing. Now that we know that you won’t have to go door to door selling Salesforce user licenses we can start talking about how to learn the platform and start a new career.

Success! You're on the list.

The Last Easy Day Was Yesterday.

I am not sure that the world needs one more blog about Salesforce or programing. But I wanted to approach this blog as a tool for people like me: veterans and career changers. As someone who has gone through the challenges of leaving the military after a 20 year career and inventing a second career in my 40s I wanted to create a blog to share what I learned along that way, and just as importantly -continue to learn.

In 2016 I retired from the Army after 21 years of active duty. I knew I wanted to do something completely different but I was not sure what. Becoming a developer never crossed my mind. That was for geniuses and I didn’t know the difference between HTML and Java. (Not kidding)

Like a lot of Salesforce careers mine began by accident and then acquired purpose as I realized that I enjoyed the work and my skills were in demand. It was a confusing path for me. I remember discovering trailhead and deciding “Awesome, I am going to be a developer. Look how much money they get paid!” But I as I dug into the modules and realized that I had no idea what a class or a method was, never mind this ‘Public Static Void ‘ nonsense. But I made it -eventually. And I wanted to share what worked for me along the way and hopefully the next group can find there way from A to B just a little faster than I did. There is no easy button, but you can do it.

Success! You're on the list.