SOQL query to fetch all fields of an object

Just in case if we need to fetch all fields with SOQL in salesforce, what we will do? SOQL query to fetch all fields of an object can be achieved with below approach.

  1. Write each field in SOQL
  2. Go to developer console, open the object, select all fields and click on query, then copy all fields from query tab.
  3. Write code in such a way that it will fetch all fields.

Now lets consider pros and cons of each approach. In first approach “Write each field in SOQL”, if any new field is created in future then code update required. Also we its time taking if there are too many fields.

In Second Approach, if any new fields is created in future then code update required.

In Third approach where we are writing code such that code doesn’t need any update if any new field added.

Apex Code

Below is the apex method for getting SOQL query to fetch all fields of an object.

This method can be used as below.

Hope this will help and increase productivity of developers.