Activity › Forums › Salesforce® Discussions › How to get all the field names of an object using SOQL?
Tagged: Field Name, Field Type, Object Field, Object Field Name, Salesforce Development, Salesforce SOQL, SOQL
-
How to get all the field names of an object using SOQL?
Posted by kapil on March 21, 2018 at 8:36 AMHow to get all the field names of an object using SOQL?
Parul replied 7 years, 7 months ago 5 Members · 5 Replies -
5 Replies
-
Hi Kapil,
The below code snippet will help you
Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(‘Account‘).getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();
system.debug(‘@@@API Name : ‘ + dfield.getname());
system.debug(‘####Label Name : ‘ + dfield.getLabel ());
}From this you will get the API Name as well as Label Name. For another sObject replace Account with your sObject.
Hope this helps you.
- [adinserter block='9']
-
Hi Kapil,
you may use this,
String ObjId = stdController.getId();
DescribeSObjectResult describeResult = Id.valueof(ObjId).getSObjectType().getDescribe();
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );
String query =’ SELECT Account.Name,’ +String.join( fieldNames, ‘,’ ) +’ FROM ‘ +describeResult.getName() +’ WHERE ‘ +’ id = :ObjId ‘ +’ LIMIT 1 ‘;
List<Contact> records = Database.query( query );Hope it helps 🙂
-
Hi Kapil,
Try the follwing code
Map <String, Schema.SObjectType> schemaMapofAllSobject = Schema.getGlobalDescribe();
Map <String, Schema.SObjectField> MapofdesiredObject = schemaMap.get(‘Contact’).getDescribe().fields.getMap();
for(Schema.SObjectField sObjectfield : MapofdesiredObject.Values())
{
schema.describefieldresult dfield = sObjectfield.getDescribe();
system.debug(‘@@@API Name : ‘ + dfield.getname());
system.debug(‘####Label Name : ‘ + dfield.getLabel ());
}Note -: Here We use Contact as our sObject.
Thanks
-
Hi,
Use standard schema class to get all fields of sobject, it also returns the properties of fields.
Map <String, Schema.SObjectType> mapSobjects = Schema.getGlobalDescribe();
Map <String, Schema.SObjectField> fieldMap = schemaMap.get(‘sobject__c’).getDescribe().fields.getMap();Hope this helps.
-
Adding more point:
You not only get the sobject name, fields but also its properties. You can use properties in your logic.
Log In to reply.