Monday, January 27, 2014

Write Apex code which will take the RecordID as input and on the basis of that it will print the Object name and field names of sObject

Write Apex code which will take the RecordID as input and on the basis of that it will print the Object name and field names of sObject

List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
}

String sampleId ='00390000003LIVw';
String prefix =  sampleId.substring(0,3);
String objectName = objectMap.get(prefix);
System.debug('** SObject Name ** '+objectName);

Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
List<String> fieldList = new List<String>();
fieldList.addAll(desResult.keySet());
for(integer i =0;i<fieldList.size();i++)
{
    System.debug('** Field Name ** '+fieldList[i]);

}

No comments:

Post a Comment