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

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...