Difference Between Count() and Count(fieldName) in SOQL Query

Difference Between Count() and Count(fieldName) in SOQL Query

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Count()

The COUNT() method in SOQL (Salesforce Object Query Language) is used to count the number of records that meet specified criteria. It's similar to the COUNT() function in SQL and is useful when you need to know the total number of records without retrieving all of them.

COUNT() function returns the number of rows that match the filtering conditions. The resulting query result size field which returns the number of rows and the records will return null. This is important to understand.

So in short, COUNT() returns the number of items in a group, including NULL values and duplicates.

Below are some examples:

SELECT COUNT() FROM ObjectName

This will return the total number of records in the specified object.

The form you have selected does not exist.

SELECT COUNT() FROM ObjectName WHERE condition

This counts the number of records that meet the specified condition.

SELECT COUNT_DISTINCT(field_name) FROM ObjectName

This will count the number of unique values in the specified field.

Couple of important things to remember:

  • COUNT() should be the only element in the SELECT list. You can not add any other field along with COUNT().
  • COUNT() with a LIMIT clause is not allowed.
  • COUNT() with an ORDER BY clause is not allowed.
  • COUNT() with a GROUP BY clause is not allowed.

Count(fieldName)

In SOQL (Salesforce Object Query Language), the COUNT(fieldName) function is used to count the number of non-null (non-empty) values in a specific field across a set of records. This function differs from the COUNT() function, which simply counts the total number of records.

COUNT(fieldName) is particularly useful when you need to know how many records have a specified field filled in.

Again in short, COUNT(expression) evaluates an expression for each row in a group and returns the number of non-null values.

Here's a bit more detail on how to use COUNT(fieldName):

SELECT COUNT(fieldName) FROM ObjectName

This counts the number of records where fieldName is not null in the specified object.

SELECT COUNT(fieldName) FROM ObjectName WHERE condition

This counts the number of records that meet the specified condition and have a non-null value in fieldName. Now Let's understand few examples:

SELECT COUNT(Email) FROM Contact

This one will count the number of contacts with a non-empty email field.

SELECT COUNT(BillingCity) FROM Account WHERE BillingCity = 'San Francisco'

This one will count the number of accounts in a specific city.

Why Should We Use Count(fieldName) ?

We can include multiple COUNT(fieldName) items in a SELECT clause. 
For example, the below query returns the number of Cases, also returns  the number of Cases associated with an Account.

SELECT COUNT(Id), COUNT(AccountId) FROM Case

Since we can not use COUNT() with a GROUP BY, but we can use COUNT(fieldName) with a GROUP BY clause. 

For example, the below query returns the number of Cases for each Case Origin value :

SELECT COUNT(Id), Origin FROM Case GROUP BY Origin

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

Comments

Understand Experience Cloud License Types

Understand Experience Cloud License Types

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

In today's fast-paced digital world, the essence of business success often lies in robust community engagement and streamlined customer experiences. Enter Salesforce Experience Cloud, a dynamic platform designed to revolutionize the way businesses connect with customers, partners, and employees. This blog post will explore different license types available for Salesforce Experience Cloud.

From fostering vibrant community networks to enhancing customer service efficiency, Salesforce Experience Cloud stands as a beacon of innovation in customer relationship management. 

The form you have selected does not exist.

Why Do We Need Licenses?

This is a very good question. You can create 100 Experience Cloud sites without buying licenses if you are in Enterprise, Performance and Unlimited orgs. But you need to purchase licenses to use specific templates and functionalities.

Give Access To Experience Cloud Sites

Generally there are two types of access: authenticated and unauthenticated. Authenticated users can log in, whereas unauthenticated users are known as guest users.

You need to buy licenses to provide your external users login access to your experience cloud sites. When you will buy licenses, you can create external profiles to provide different levels of permissions to your external users.

Experience Cloud License Types

In Salesforce, there are 5 different types of experience cloud licenses. They are -

  • External App
  • Customer Community
  • Customer Community Plus
  • Partner Community
  • Channel

External App License Type

You can choose to buy External App licenses for member accounts or Salesforce objects based on your business requirements. Besides the guest user profile, you can create more external profiles with these licenses to further improve the functionality of your website. These profiles will make it possible for authorized users to visit your websites, resulting in a seamless user experience.

Customer Community License Type

The Customer Community license is intended for clients who must regularly communicate with your company. Customers that own this license can access services like cases, knowledge articles, and communities by logging in to your Salesforce Experience Cloud website.

Users of the customer community have the ability to access and modify their own data, including support cases and account details. Through communities, they can work together and take part in conversations, polls, and events with other people.

Customer Community Plus License Type

Salesforce provides the Customer Community Plus license in addition to the Customer Community license. Beyond what the Customer Community license offers, this license offers more tools and capabilities, such as automation and access to Salesforce assets. Custom objects, processes, and custom integrations with external systems can all be built with the Customer Community Plus license. Remember that this license costs more than the Customer Community license, and that only those companies who truly need the extra features and functionalities it offers are advised to purchase it.

Partner Community License Type

For partners and other external users who must work with your company, there is a Partner Community license. Partners can see or change work-related data on your Salesforce Experience Cloud site with this license.

Users of partner communities can interact with other users through communities and access services like leads, opportunities, and cases. In addition, they are able to generate and amend work-related documents like partner accounts and deals.

Channel License Type

It is useful for business to business communities and portals that calculate their usage on the basis of numbers of partners instead of numbers of individual users.

Comparison

In the blow section, I would like to share a high level comparison between these license types. This is important if you are planning to go for Experience Cloud Certification or if you are working on experience cloud project.

 

External AppCustomer CommunityCustomer Community PlusPartner CommunityChannel
AccountsRead, EditRead, EditRead, Create, EditRead, Create, EditRead, Create, Edit
ContactsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
CasesRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
OpportunitiesRead, Create, EditRead, Create, Edit
LeadsRead, Create, EditRead, Create, Edit
CampaignsRead, Create, EditRead, Create, Edit
AssetsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
ContractsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
EntitlementsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
OrdersRead, Create, Edit, DeleteRead, Create, Edit, DeleteRead, Create, Edit, DeleteRead, Create, Edit, DeleteRead, Create, Edit, Delete
ProductsRead OnlyRead OnlyRead OnlyRead OnlyRead Only
Price BooksRead OnlyRead OnlyRead OnlyRead OnlyRead Only
ReportsCreate, ManageCreate, ManageCreate, Manage
DashboardsRead OnlyRead OnlyRead Only
ListViewsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit
DocumentsRead OnlyRead OnlyRead OnlyRead OnlyRead Only
External ObjectsRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, EditRead, Create, Edit

 

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

Comments

Return Type of SOQL and SOSL Queries in Salesforce

Return Type of SOQL and SOSL Queries in Salesforce

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Introduction Of SOQL

SOQL, or Salesforce Object Query Language, is a query language used to interact with the data stored in Salesforce. It is similar in syntax to SQL, the standard database query language, but is specifically designed for Salesforce data.

SOQL enables developers to construct simple to complex queries against the data in Salesforce. This includes searching, retrieving, and manipulating records stored within Salesforce's cloud-based platform. It is particularly useful for working with large data sets where you need to quickly find specific records that meet certain criteria.

One of the key features of SOQL is its ability to traverse relationships between objects. In Salesforce, data is stored in objects (akin to tables in a relational database). SOQL allows you to query not just a single object, but also to join related objects and access their fields in a single query. This makes it extremely powerful for extracting related data across different parts of the Salesforce database.

The form you have selected does not exist.

Introduction Of SOSL

SOSL, or Salesforce Object Search Language, is a specialized search language developed by Salesforce for searching text, email, and phone fields across multiple objects simultaneously in the Salesforce environment. Unlike SOQL, which is designed for querying specific objects and fields, SOSL is designed to perform text-based search queries across large datasets and multiple objects at once.

The primary use of SOSL is to quickly search for specific terms or phrases within Salesforce records. It's particularly useful when the exact location (i.e., in which object or field) of the data is unknown or when the data is spread across multiple objects. For example, if you need to find a contact's name, an account detail, or a specific email content, SOSL can search across all relevant objects and fields in one go.

SOSL can return results from multiple objects and is highly efficient in situations where a global search is needed. It is often used in global search bars in Salesforce applications, allowing users to search for data across the entire Salesforce database.

While SOQL is more about querying and retrieving structured data from specific objects and fields, SOSL is about searching unstructured text across multiple objects. Both SOQL and SOSL have their unique use cases and are integral to effective data management and retrieval in Salesforce applications. Developers and administrators use SOSL to build dynamic search capabilities into their Salesforce applications, enhancing the user experience and data accessibility.

Return Type of SOQL

A SOQL querty can only return a single record, a list of records, or an Integer, provided COUNT method is being used. 

You can always assign the query to a list of sobjects, then check to see how many elements are there in the list like shown below

List<Account> accountQuery = [SELECT Id,Name FROM Account]; 
if (accountQuery.size() > o) { 
 // execute code 
}

Return Type of SOSL

A list of lists of sObjects, each containing the search results for a certain sObject type, is returned by SOSL commands. The sequence in which the SOSL query provided the result lists for the SOSL statements is always maintained when they are returned.
An empty list for the specified sObject type appears in the search results when an SOSL statements query returns no entries for that sObject type.

List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Contact, Opportunity, Lead];
Account [] accounts = ((List<Account>)searchList[0]);
Contact [] contacts = ((List<Contact>)searchList[1]);
Opportunity [] opportunities = ((List<Opportunity>)searchList[2]);
Lead [] leads = ((List<Lead>)searchList[3]);

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

Comments

How do I know whether my instance is migrated into Hyperforce?

How do I know whether my instance is migrated into Hyperforce?

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Hyperforce is the next-generation Salesforce infrastructure architecture, built for the public cloud. Hyperforce infrastructure is composed of code rather than hardware, so that the Salesforce platform and applications can be delivered rapidly and reliably to locations worldwide, giving customers more choice and control over data residency. Salesforce products running on Hyperforce benefit from its integration of enhanced standards for compliance, security, agility, and scalability, and from Salesforce’s continued commitment to privacy. By providing a common foundation for deploying our application stacks, Hyperforce accelerates Salesforce’s ability to innovate across product clouds and deliver additional business value to customers.

I have published a blog post earlier covering basics of Hyperforce. You can read the post by clicking on the link below.

What is Hyperforce?

Now with this migration happening, the question is how do I know whether my org has migrated into Hyperforce or not?

In this post, I am going to show you how you can quickly find out the answer.

The form you have selected does not exist.

First Find Out The Instace of Your Org

To find out the instance of the org, what you need to do is go to Setup and from there go to Company Information. You will be able to find out the instace number from this page. Screenshot attached.

Find Out

With the instance number, you need to go to the page https://availability.salesforce.com/find-my-instance/

In this page, put the instance number and submit. If your org is already migrated into Hyperforce, you will find it from here. Screenshot attached

 

Find Out The Status Of Your Sandboxes

You can go to Settings -> Environments -> Sandboxes to check whether your sandboxes are in Hyperforce Server or not. See the screenshot below –

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

Comments