Advanced SOQL Queries for Salesforce Metadata

Prakash DasPrakash Das
2 min read

Salesforce Object Query Language

Salesforce's SOQL (Salesforce Object Query Language) is like having a magic wand for querying your Salesforce data and metadata! Whether you’re an admin, developer, or architect, mastering SOQL can make you a data wizard, helping you uncover comprehensive metadata information and gaining deeper insights into your Salesforce data models. Here are some spellbinding advanced SOQL queries to get you started on your journey to SOQL mastery!

1. Fetch Developer Names and Data Types for Account Fields

SELECT DeveloperName, DataType 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account'

2. Fetch Fields with Specific Data Types for a Given Object

If you need to filter fields by specific data types, such as 'Text' or 'Number', this query will be useful:

SELECT DeveloperName, DataType 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
AND DataType IN ('Text', 'Number')

3. Fetch Fields with a Specific Label Keyword

To find fields with a label containing a specific keyword, like 'Name', use the following query:

SELECT DeveloperName, DataType, Label 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
AND Label LIKE '%Name%'

4. Fetch Fields Created by a Specific User

Retrieve fields created by a specific user by using their user ID:

SELECT DeveloperName, DataType, CreatedById 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
AND CreatedById = '005XXXXXXXXXXXx'

5. Fetch Custom Fields Only for a Given Object

To list only the custom fields for the Account object, use this query:

SELECT DeveloperName, DataType 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
AND IsCustomField = true

6. Fetch All Fields Including Their Length and Precision

For a more detailed view of fields, including their length and precision, use the following query:

SELECT DeveloperName, DataType, Length, Precision 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account'

7. Fetch Fields Along with Their Help Text

To get fields that include their help text (description), use this query:

SELECT DeveloperName, DataType, Description 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
AND Description != null

8. Fetch Fields Sorted by Creation Date

To sort fields by their creation date, you can use this query:

SELECT DeveloperName, DataType, CreatedDate 
FROM FieldDefinition 
WHERE EntityDefinition.QualifiedApiName = 'Account' 
ORDER BY CreatedDate DESC

🚀 Conclusion

Mastering these advanced SOQL queries will significantly enhance your ability to manage and understand your Salesforce data models. By retrieving comprehensive metadata information, you can make more informed decisions and improve your organization's data architecture. Happy querying!

0
Subscribe to my newsletter

Read articles from Prakash Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prakash Das
Prakash Das

Software Engineer, focused on building and designing accessible, human-centered products and digital experiences