~

Query all fields in an object using SOQL

Explains and shared reusable and efficient way to get record type Id in apex.

Query all fields in an object using SOQL

Currently Salesforce does not allow you to query all fields in an object using wild card selectors. Even though it is usually not a best practice to query all fields in an object, sometimes it is useful to query all fields for debugging purposes

Simple way to query all fields in a object in single line is like below,

Database.query('SELECT ' + String.join(new List(Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().keySet()), ',') + ' FROM Contact');

This will be very handy while debugging. But please note that this is not the optimal way of doing this. If you are doing it in multiple places in code, it is recommended to cache the describe calls. An example of caching Schema.getGlobalDescribe() can be found in the blog How to get RecordType Id efficiently in Apex with caching

[Top]

Comments

    No comments yet. Be the first to share your thoughts!

Phone

Office: +1 725 333 6699

Email

Office: admin@appcolab.com

Site: https://appcolab.com

Social
©2024 AppColab LLC · All rights reserved.