Activity › Forums › Salesforce® Discussions › How to get all the fields of Sobject using Dynamic Salesforce Apex?
Tagged: Apex PageBlockTable, Dynamic Apex, Salesforce Apex Code, Salesforce sObject, Schema GetGlobalDescribe, Schema sObjectType
-
How to get all the fields of Sobject using Dynamic Salesforce Apex?
Posted by Anurag algoworks on September 19, 2018 at 7:55 AMHow to get all the fields of Sobject using Dynamic Salesforce Apex?
Aman replied 7 years, 7 months ago 5 Members · 4 Replies -
4 Replies
-
Hi,
You can use the methods of schema class. Below is a snippet of code that you can use :
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get(‘API_Name_Of_SObject’) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap();
Thanks
- [adinserter block='9']
-
Hello Anurag,
Try this code
`
public class ControllerClassName
{
public ListstrList { get;set; } public void autoRun()
{
MapobjectFields = Schema.getGlobalDescribe().get(‘Account’).getDescribe().fields.getMap(); strList = new List
(objectFields.keySet()); }
}
`
Thanks. -
You can use this Apex code with Vf page :
<apex:page controller=”GetSobjectDynamic” action=”{!autoRun}”>
<apex:form>
<apex:pageblock>
<apex:pageblockTable value=”{!strList}” var=”obj”>
<apex:column value=”{!obj}”/>
</apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>
………..
public class GetSobjectDynamic
{
public List<String> strList { get;set; }public void autoRun()
{
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(‘Account’).getDescribe().fields.getMap();strList = new List<String>(objectFields.keySet());
} }
Hope thsi will help you.
Thanks
-
Hi,
Below is a snippet of code that you can use :
Account account = [select Id, AnnualRevenue, Website from Account limit 1];
Map<String, Schema.SObjectField> schemaFieldMap = Schema.SObjectType.Account.fields.getMap();
Map<String, Object> queriedFieldValues = new Map<String, Object>();
for (String fieldName: schemaFieldMap.keySet()) {
try {
queriedFieldValues.put(fieldName, account.get(fieldName));
} catch (SObjectException e) {
// Intentional capture
}
}
System.debug(queriedFieldValues);
Thanks.
Log In to reply.