How to get fields for sObject using Apex?

Sample Code:

public String obj {get;set;} public List<sObject> objLst {get;set;}
public List<String> objFields {get;set;}
Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
Schema.sObjectType objType = globalDescription.get(obj);
Schema.DescribeSObjectResult r1 = objType.getDescribe();
Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();

for(Schema.SObjectField field : mapFieldList.values())
{
Schema.DescribeFieldResult fieldResult = field.getDescribe();
if(fieldResult.isAccessible())
{
objFields.add(fieldResult.getName());
}
}

Written by 

2x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*