Skip to main content

SOQL TIP : Use FIELDS() for Dynamic Field Retrieval with Complete Guide & Examples

 


Salesforce introduced one of its most powerful and developer-friendly features in Winter ’23: the FIELDS() function in SOQL.

This feature completely changes how we write queries—especially for debugging, API testing, dynamic data exploration, and admin/developer productivity.

Before this feature, retrieving “all fields” from an object required:

  • manually listing every field

  • generating describe calls

  • writing dynamic SOQL

  • using tool-generated queries (Workbench, VS Code, etc.)

Now, with FIELDS(), you can retrieve standard fields, custom fields, or ALL fields in one line. Click here to watch video on how it works



What is the FIELDS() Function?

FIELDS() is a SOQL function that dynamically expands into the fields of an SObject at query runtime.

It supports three modes:

ModeDescription
FIELDS(STANDARD)Retrieves all standard fields of the object.
FIELDS(CUSTOM)Retrieves only the custom fields (fields ending in __c).
FIELDS(ALL)Retrieves both standard + custom fields.

Why Is FIELDS() So Useful?

✔ Saves time — no need to list 50+ fields

✔ Perfect for debugging

✔ Removes need for dynamic SOQL in many cases

✔ Excellent for API exploration (Postman, Workbench, VS Code)

✔ Helps admins who need quick data visibility

✔ Reduces typing + chance of errors


Basic Syntax

SELECT FIELDS(ALL) FROM Account LIMIT 1

Very simple, but extremely powerful.


Use Case 1: Retrieve ALL Fields from an Object

This is the most popular and practical use case.

Example: Retrieve All Fields From Account

SELECT FIELDS(ALL) FROM Account WHERE Id = '001XXXXXXXXXXXX'

This returns:

  • all standard fields (Name, CreatedDate, OwnerId…)

  • all custom fields (Rating__c, Customer_Segment__c…)

  • formula fields

  • lookup fields

  • address fields

  • system fields


Use Case 2: Retrieve ONLY Standard Fields

Useful when dealing with API integrations, clean datasets, and performance optimization.

Example

SELECT FIELDS(STANDARD) FROM Contact LIMIT 10

This returns only standard fields like:

  • FirstName

  • LastName

  • AccountId

  • Email

  • CreatedDate

  • LastModifiedDate

All custom fields (__c) are excluded.


Use Case 3: Retrieve ONLY Custom Fields

Perfect for:

  • org audits

  • metadata reviews

  • cleaning technical debt

  • understanding customization level

  • analyzing managed package fields

Example

SELECT FIELDS(CUSTOM) FROM Opportunity LIMIT 5

This returns fields like:

  • Region__c

  • Probability_Score__c

  • Is_Renewal__c

  • Product_Code__c

No standard fields are included.


Use Case 4: Debugging Triggers and Apex Code

When you're debugging a trigger or flow that behaves strangely, retrieving all fields helps you verify what’s happening.

Example: Quick Debug Query

System.debug([ SELECT FIELDS(ALL) FROM Case WHERE Id = :caseId ]);

This prints the entire Case record—including fields you forgot existed.


Use Case 5: Exploring Data Model of Unknown Objects

Perfect for:

  • newly inherited orgs

  • client orgs

  • managed packages

  • third-party integrations

  • sandboxes with changes

Example: Explore Custom Object Fields

SELECT FIELDS(CUSTOM) FROM Project__c LIMIT 1

Quickly shows you all customizations related to that object.


Use Case 6: Querying in Developer Console / Workbench / Postman

For API testing:

GET /services/data/v59.0/query?q=SELECT+FIELDS(ALL)+FROM+Lead+LIMIT+1

Great for external system developers who need a full understanding of the schema.


Use Case 7: Exporting Data Fast

Admins who want a quick snapshot can use:

SELECT FIELDS(ALL) FROM User LIMIT 200

Then export the results—no need to build CSVs manually.


Limitations of FIELDS() You Should Know

Although powerful, FIELDS() has rules:

Not Allowed in Triggers

Triggers require specific field access, not dynamic field enumeration. So this:

SELECT FIELDS(ALL) FROM Account

cannot be inside trigger context Apex.


Cannot Be Used for Bulk Select

If the object has too many fields, the query might hit SOQL limits.
For example: 500+ fields may exceed query character limits.


Not Usable in Subqueries

This will fail:

SELECT Id, (SELECT FIELDS(ALL) FROM Contacts) FROM Account

Not Accepted in Tooling or Metadata API Queries

It works only in standard SOQL.


Performance Considerations

Using FIELDS(ALL) means:

  • more data transferred

  • more CPU time during serialization

  • larger heap size

  • slower performance in large orgs

Recommendation:

Use FIELDS(STANDARD) or FIELDS(CUSTOM) when possible.


Best Practices

  • Use FIELDS() for debugging, not production
  • Limit your results
    • Always include LIMIT:
    • SELECT FIELDS(ALL) FROM Opportunity LIMIT 1

  • Avoid in Apex classes that run frequently
  • Better to list fields explicitly for optimization.
  • Use in developer console, quick scripts, API testing


Comments