Sunday, 30 December 2012

Getting All fields from an object using dynamic apex

Here in the following snippet

Schema.getGlobalDescribe() returns the map with all sobject types in our org

By using this we can get Sobject type based on api name of object as in second line of snippet.

After getting the sobject type we can get the all the describe result of that object using "getDescribe()" method as in the third line of snippet.

Then we can get all field from that object by using "fields.getMap" as in the the
fourth line of snippet.

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

No comments:

Post a Comment

Getting Reference FieldsI(Lookup or Master detail) through dynamic apex

Hi , We  had a scenario If  we get a parameter "id" in the  url  then we should  be  able to find out    object reference is ...