How to Select All Fields with SOQL in Apex




How to query all fields from an object by exploiting the metadata available from the DescribeSObjectResult class.

Code

DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();

List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );


String query = ' SELECT ' + String.join( fieldNames, ',' ) +' FROM '+ describeResult.getName();
List<SObject> records = Database.query( query ); System.debug( records );
Debug

22:42:58:132 USER_DEBUG [13]|DEBUG|(Account:{Id=0012800000nY6VqAAK, IsDeleted=false, Name=sForceTest1, BillingStreet=The Landmark @ One Market, BillingCity=SanFrancisco, BillingState=CA, BillingPostalCode=94105, BillingCountry=US, BillingAddress=API address [ The Landmark @ One Market, SanFrancisco, CA, 94105, US, null, null, null, null, null], ShippingAddress=null, Phone=(415) 901-7000, Website=http://www.sforcetest1.com, PhotoUrl=/services/images/photo/0012800000nY6VqAAK, OwnerId=00528000001Dmp3AAC, CreatedDate=2016-08-12 13:34:29, CreatedById=00528000001Dmp3AAC, LastModifiedDate=2016-08-12 13:34:29, LastModifiedById=00528000001Dmp3AAC, SystemModstamp=2016-08-12 22:24:30, IsPartner=false, IsCustomerPortal=false, CleanStatus=Pending, Potential_Value__c=0.00, Allowed_Support__c=false, Match_Billing_Address__c=false}, ,.....

Cheers!

0 comments:

Post a Comment