Home How to get fields for sObject using Apex?

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());
}
}

Leave a Comment

*