I am trying to build a VF component. One of the feature of the component is the ability to show the fields related to an object dynamically. For example - if my component is used in an VF page like:

<apex:page controller="Acntrller">
<c:SampleComponent Record="Account"/>    

Then I should be able to get the fields related to Account in the custom controller built for VF component.The VF component takes record type(object type) and assign it to a variable controllerValue, which is further used in my controller.

<apex:attribute name="record" description="The type of record we are viewing."
type="String" required="true" assignTo="{!controllerValue}"/>

What I want: I want a way to retrieve the fields of the object mentioned in the VF page i.e. <c:SampleComponent Record="Account"/> ( Account object).

What I know: I found that there is a way to get the fields using : Map<String, Schema.SobjectField> fields = Schema.SobjectType.Account.fields.getMap() , but the problem here is it is static( the account part cannot be replaced with a variable- controllerValue) and I need something dynamic to get the fields.

Is there a way to get the fields of corresponding object dynamically?

  • Which records do you want to retrieve? – Adrian Larson Dec 6 '16 at 23:04
  • I get all the records of the object. I would like to retrieve fields of the object mentioned in : <c:SampleComponent Record="Account"/> . Ex- I want get the field of Account object. – SfdcBat Dec 6 '16 at 23:15
  • Why not just use <apex:enhancedList type="Account" />? – Adrian Larson Dec 6 '16 at 23:22
  • 1
    @AdrianLarson <apex:enhancedList> doesn't support lightning exp and salesforce1 so personally I wouldn't recommend using it for now. I am planning to remove them from our projects. – Lance Shi Dec 6 '16 at 23:27
  • My idea is to build a reusable component. I can specify any object and fetch the records of the object and show it as a list view. Also user has an ability to select the field which they want to view in the list view, There is more feature to this but this is a starting point of what I want to do. – SfdcBat Dec 6 '16 at 23:28

Try the following code:

String sobject_type = 'Account';
Map<String, Schema.SObjectType> global_describe = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> object_fields_map = global_describe.get(sobject_type).getDescribe().fields.getMap();

Let me whether it resolves your issue.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.