Salesforce SOQL Query RecordType

In Salesforce, RecordTypes allow us to group records of the same type for an object. It will enable the customization of page layouts, fields, required fields, and picklist values for different user profiles, as well as restrict field access for layouts and the creation of records. 

For example, you can create record types to ensure that a team only sees customer accounts, not partner ones. 

Each RecordType in Salesforce is represented by the RecordType object and is associated with standard or custom objects, such as accounts, opportunities, or custom objects.

SOQL Queries to Get RecordType

In the examples below, we will discuss scenarios where we execute SOQL to retrieve the record type names, IDs, and developer names of different objects.

Get Recordtype Name and ID Using SOQL Query

In the SOQL query below, we will retrieve the name and ID of the record type from the account object.

SOQL query:

SELECT Id, Name FROM RecordType WHERE SobjectType = 'Account'

Output:

Get record type name in SOQL Salesforce

Get Specific RecordType Details in SOQL query

In the SOQL query below, we retrieve active record types for the Case object, including their name, ID, and developer name, from the active record types.

SOQL query:

SELECT Id, Name, DeveloperName FROM RecordType WHERE SobjectType = 'Case' AND IsActive = true

Output:

Get details of record types in Salesforce SOQL

Get Salesforce Object Records Assigned to a Record Type using SOQL

Using the SOQL query, we can also retrieve the records related to a specific record type in the Sobject.

Using the SOQL query below, we have retrieved Case records assigned to a specific record type by filtering the records by DeveloperName.

SOQL query:

SELECT id, Priority FROM Case WHERE RecordTypeId IN (SELECT Id FROM RecordType WHERE DeveloperName = 'Sales_Support')
Get records Assigned to record type in Salesforce SOQL

After executing the above query, we retrieved the case records assigned to the record type with the developer name Sales_Support.

Get Records from Record Type Name in Salesforce SOQL

Instead of retrieving records from record type ID or developer name, we can also fetch the record details of a specific record type by its name using RecordType.Name in the SOQL query.

SOQL query:

SELECT Id, Subject FROM Case WHERE RecordType.Name = 'Product'

Output:

Get Record type by Name in Salesforce SOQL

In the above SOQL query, we have retrieved the case record details, such as subject and ID, where the record type name is “Product.”

Retrieve Record Types of all Objects in Salesforce SOQL

When you need to retrieve all the record types in your Salesforce org, you can execute the SOQL query below to fetch the details of the record types.

SOQL query:

SELECT Id, SobjectType, Name, DeveloperName FROM RecordType

Output:

SOQL query to get all record types in Salesforce

As we can see in the output, we have received all record types with their details in the Salesforce org.

Count the number of records assigned to each record type in an object using SOQL.

For example, if you have an object with many record types and want to track the number of records assigned to each record type, then you can execute the SOQL query below.

SOQL query:

SELECT RecordType.Name, COUNT(Id) 
FROM Case 
GROUP BY RecordType.Name

Output:

Count number of records assigned to record type in Salesforce SOQL

In the above query, we have counted the number of records assigned to different record types of the Case object. In the output, we can see the count of records assigned to each record type.

This way, we can query sObject record type details in Salesforce SOQL queries by following the above methods.

Conclusion

In this Salesforce tutorial, we learned how to execute SOQL queries to retrieve and analyze record types and their associated records across various objects, such as Accounts and Cases. These queries allow for filtering by RecordType ID, DeveloperName, or Name, providing ease in retrieving the data.

By following the above methods in relation to specific scenarios, you can execute SOQL queries efficiently and retrieve information related to the record types.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.