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

No comments:

Post a Comment