Why does Apex SOQL return RecordTypeId automatically?

When writing SOQL inside Apex, you might notice that Salesforce sometimes returns additional fields in the result even though they were not explicitly queried. A very common example is the RecordTypeId field. Developers are often surprised to see this in debug logs, especially when the SOQL query clearly did not include this field.

Consider this example, executed in Apex:

List<Account> lstAccount = [SELECT Id, Name, BillingStreet FROM Account LIMIT 1];
System.debug(lstAccount);

You might see debug output similar to this:

(Account:{Id=001..., Name=Acct_nm001, BillingStreet=xyz Road, RecordTypeId=012...})

Even though the SOQL did not select RecordTypeId, the result still contains it.

But if you run the same query in the Query Editor inside Developer Console, the RecordTypeId field does not appear.

This leads to the question: why does Apex include it automatically?

Explanation

Apex and the SOQL engine perform certain automatic features under the hood. When an SOQL query is executed from Apex code, Salesforce includes some default behavior that may not happen in the Query Editor. Internally, Apex needs certain base fields so the platform features continue to work properly.

Salesforce automatically adds fields like Id and RecordTypeId to queries executed from Apex. This is done because these fields are essential for many internal operations in the platform. Visualforce rendering, dynamic forms, and various internal APIs may require the record’s type information, so Salesforce ensures it is present.

This behavior is by design.

Hidden SOQL modifications in Apex

The SOQL engine modifies your query before execution. For example, Apex silently inserts filters and limits.

If you execute a SOQL query without specifying ALL ROWS, Apex automatically adds:

WHERE IsDeleted = FALSE

Similarly, the platform injects a default LIMIT value based on your remaining governor limits.

As a result, what you write in code is not always exactly what the SOQL engine executes.

Example:

Account[] accounts1 = [
    SELECT Id, RecordTypeId, Name,
    (SELECT Id, RecordTypeId, Name FROM Contacts WHERE IsDeleted = FALSE)
    FROM Account
    WHERE IsDeleted = FALSE
    LIMIT :1+Limits.getLimitQueryRows()-Limits.getQueryRows()
];

This is almost equivalent to writing:

Account[] accounts2 = [
    SELECT Name,
    (SELECT Name FROM Contacts)
    FROM Account
];

Apex simply adds fields to support system logic.

Why include RecordTypeId but not audit fields?

Salesforce has never published official documentation that explains exactly why this field is auto-included, but the most accepted explanation in the community is that certain parts of the platform rely heavily on this field. For example, Visualforce dynamic rendering of picklists based on Record Types requires knowing the record type for each row.

Audit fields (such as CreatedDate, CreatedById, etc.) are not always needed internally, so they are not included automatically.

At some point, Salesforce introduced internal functionality that required presence of RecordTypeId, and therefore started automatically including it for all objects in Apex SOQL queries.

Job-Oriented Salesforce Training with 100% Money Back Guarantee

Our Salesforce Course is designed to offer a complete understanding of the Salesforce platform, providing you with the necessary skills to excel in the CRM industry. The program covers essential modules such as Salesforce Admin, Developer, and AI, combining theoretical knowledge with practical experience. Through real-world projects and assignments, you’ll develop the expertise needed to solve complex business challenges using Salesforce solutions. Our expert instructors ensure you gain both technical skills and valuable industry insights to succeed in the Salesforce environment.

Along with technical expertise, our Salesforce Training in Bangalore offers personalized mentoring, certification preparation, and interview coaching to improve your career prospects. You’ll have access to comprehensive study resources, practical project experience, and continuous support throughout your learning journey. By the time you complete the course, you’ll be well-equipped for certification exams and capable of applying your problem-solving skills in real-world scenarios. Begin your Salesforce career with us and unlock limitless career opportunities. Enroll for a Free Demo today!

0
Would love your thoughts, please comment.x
()
x