+ Start a Discussion
SpunkySpunky 

Method to retrieve all fields on an object in apex class

I have a custom controller that queries 3 objects for fields.

 

For my custom object called Job_Aids__c, is there a way of retrieving all the field names instead of having to list them all out as I have below ?

 

I hope the answer is yes because I have over 300 fields. Please help

public Job_Aid__c getJob_Aid() {
         return [select id, createddate,U_opportunityname__c,X3rd_party_pressuring_improved_bus_perf__c,text__c from Job_Aid__c  where U_opportunityname__c = :ApexPages.currentPage().getParameters().get('id')]; 
    } 

 

WizradWizrad

Yes, you can do this, but I don't think its going to be your end-all solution.

 

You can use Describe calls to get the api names of all fields and then you can build out your query as a string and execute it.

 

But there is no SELECT * for a reason...cause you're probably going to hit governor or even oracle db limits querying for 300 fields at a time.

 

Might need to do multiple queries and merge them together, or ask yourself "do I need all 300 fields?".