Skip to main content

SOQL as a Summary Report

When learning SOQL, I found it helpful to relate things back to something I was familiar with. I'll be using Summary Reports to help explain how to use GROUP BY and GROUP BY ROLLUP in SOQL.

First, a Salesforce Report

Let's start with a Salesforce Report of Accounts grouped by Billing State/Province:


To get this, I created a Report with the Accounts standard report type. Then I created the grouping, added some fields and ran the report.

Writing SOQL

I'll be executing my SOQL via the SFDX Command Line Interface (CLI). If you aren't familiar with this, check out my other post - SOQL from the Command Line

Feel free to use your preferred method to run your SOQL. If you don't have a preferred method, I talk about my favorites in the last section of another post - Why should I use SOQL?

GROUP BY

Here's our example Salesforce Report done via SOQL:


The key difference is that the Salesforce Report gives us data about each record and SOQL just gives us the totals for each grouping. Depending on what you're trying to do, the additional data given by a Salesforce Report may make it the right tool for the job.

In SOQL, we are
  1. SELECT'ing the BillingState field as the field to group on
  2. COUNT'ing the number of Ids returned for each record in the group
  3. Getting data FROM the Account object
  4. GROUP'ing BY the BillingState field
You'll notice that we get a null BillingState in our result. We could have added a WHERE Clause and filtered out null values, but that's a lesson for another day.

GROUP BY ROLLUP

Salesforce Reports have the ability to create nested groupings. For example, if we took our sample report and added Type as a grouping we would see Accounts by Billing State/Province and then by Type:


We can do the same thing in SOQL by using GROUP BY ROLLUP:


In SOQL, we are
  1. SELECT'ing the BillingState and Type fields as the fields to group on
  2. COUNT'ing the number of Ids returned for each record in each group
  3. Getting data FROM the Account object
  4. GROUP'ing BY ROLL'ed UP values of the BillingState and Type fields
I must say, the results here are not super readable at first. The trick is to focus on any fields you set as sub-groups, reading down them until you reach the last null result for the sub-group. This will be the total number of records for the primary grouping.

Let's take CA BillingState as an example. The last null result in the Type column shows a count of 2. This is what we expect because the sample report above has 2 CA Accounts.

The CA BillingState example is an interesting one because it has 2 null results in the Type column. Looking back at our sample report, we can see that the sForce account has a null Type and it is sub-grouped below the CA BillingState. The result of our GROUP BY ROLLUP is reading the null Type and setting it as a sub-group result in the Type column. The Salesforce Report and SOQL are doing the same thing here, they're just displaying them differently.

Continued Learning

I hope you enjoyed this introduction to GROUP BY and GROUP BY ROLLUP in SOQL. If you'd like to go deeper, I recommend checking out these resources for some continued learning:

Comments

Popular posts from this blog

Salesforce Lighting Migration: Plan for Success

In April 2018, I led the Salesforce Lightning Migration at Postmates. This is how I would do it over again if I could.

Define Goals The goals of a Lightning Migration are: Enable Lightning ExperienceConfigure Lightning ExperienceDocument all Salesforce processesCreate training and support materialsLightning Experience is adopted Easy enough right? Let's go though the plan to see how we accomplish each one. Enable Lightning Experience Just turn it on right? Well, kinda.
Although it may be controversial, I recommend turning it on and hiding the option from all users. This can be done by removing the Lightning Experience User permission from your custom profiles. 
And yes, turn it on in production
Why? You're going to turn it on anyway. Salesforce has declared this as the path forward, so you either turn it on now or wait until they flip the switch for you.
Plus, turning this on ahead of time will make deploying from a sandbox much easier. Trust me.
Goal #1 Enable Lightning Exp…

Why Users Switch Back to Classic

When rolling out Lightning Experience, people are constantly switching back to Classic. Even after the initial rollout it's common to see people switching back and forth.

The most common reasons someone switches back to Classic are:
Classic loads fasterTheir workflow is slower in LightningThey just don't want to switch to Lightning Overcoming these objections will be key to a successful rollout. Let's dive into each one in more detail. Classic Loads Faster It does. 😢
And when you hear someone complain about this, take their side.  Agree with them.  Make that ironic joke, "haha, yeah Classic is faster than Lightning". ⚡⚡⚡
Then overcome this by highlighting how their overall workflow is faster in Lightning.
The page takes longer to load because each page is made up of many components. Once it has loaded, someone can complete their task on a single page. Their Workflow is Slower in Lightning It probably is. At least until they get used to it. 👨‍💻👩‍💻
Similarly, i…

Modify Salesforce Data from the Command Line

When doing Salesforce data loads, how often are you putting the same value in an entire column? I find this happening when I need to:
Move all accounts from one user to anotherAssign a set of leads to a specific userSet a field to a specific value Let's use the example from my other post: Salesforce Data Loads from the Command Line - Move all accounts owned by one user to another user. Magic Let's break down each piece of this: soql This is an alias I set for sfdx force:data:soql:query --query. It saves me from having to type this out each time and let's me query from the command line by just typing soql and my query.

This is a popular StackExchange post that talks about how to set an alias.
cat This command outputs the contents of a file. It is common to use this command with other commands by piping the output into another command. In our example, we are piping the output into the cut command.
This post gives more examples of how to use the cat command. cut This is a comm…