You need to sign in to do that
Don't have an account?
Ramandeep_Singh
Display Data In Visual Force Page
Hi all,
I'm trying to display field's data in VF page. Function is like, when selectting the object from picklist which is in vfp the related fields will be displayed in another picklist. On clicking the botton the field's data related to the selected picklist value should be displayed on the Vfp.
Visual Force Page:
Custom Controller:
Please let me know were am I lacking. It would be really helpful.
Thanks and Regards,
Ramandeep
I'm trying to display field's data in VF page. Function is like, when selectting the object from picklist which is in vfp the related fields will be displayed in another picklist. On clicking the botton the field's data related to the selected picklist value should be displayed on the Vfp.
Visual Force Page:
<apex:page controller="objectController"> <apex:form > <apex:pageBlock > <apex:pageBlockSection columns="4"> <apex:pageBlockSectionItem > <apex:outputlabel value="Object :"/> <apex:actionRegion > <apex:selectList value="{!selectedObject}" size="1"> <apex:selectOptions value="{!ObjectNames}"/> <apex:actionSupport event="onchange" rerender="myFields,myFields1"/> </apex:selectList> </apex:actionRegion> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputlabel value="Field 1"/> <apex:outputPanel id="myFields"> <apex:actionRegion > <apex:selectList value="{!selectedField}" size="1"> <apex:selectOptions value="{!ObjectFields}"/> </apex:selectList> </apex:actionRegion> </apex:outputPanel> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputlabel value="Field 2"/> <apex:outputPanel id="myFields1"> <apex:actionRegion > <apex:selectList value="{!selectedField2}" size="1"> <apex:selectOptions value="{!ObjectFields}"/> </apex:selectList> </apex:actionRegion> </apex:outputPanel> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:commandButton action="{!output}" value="Select"> <apex:actionSupport event="onclick" rerender="mychart"/> </apex:commandButton> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:pageBlockTable value="{!oList}" var="ac"> <apex:column value="{!ac['selectedField']}"/> </apex:pageBlockTable> </apex:pageBlockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>ERROR: Invalid field selectedField for SObject Account // On clicking
Custom Controller:
public class objectController { public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); public List<sObject> oList {get; set;} public String selectedObject {get; set;} public String selectedField {get; set;} public String selectedField2 {get; set;} Public objectController() { selectedObject = 'Account'; } public List<SelectOption> getObjectNames() { List<SelectOption> objNames = new List<SelectOption>(); List<String> entities = new List<String>(schemaMap.keySet()); entities.sort(); for(String name : entities) { objNames.add(new SelectOption(name,name)); } return objNames; } public List<SelectOption> getObjectFields() { Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject); Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap(); List<SelectOption> fieldNames = new List<SelectOption>(); for (String fieldName: fieldMap.keySet()) { fieldNames.add(new SelectOption(fieldName,fieldName)); //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label. } return fieldNames; } public List<SelectOption> getObjectFields2() { Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject); Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap(); List<SelectOption> fieldNames = new List<SelectOption>(); for (String fieldName: fieldMap.keySet()) { fieldNames.add(new SelectOption(fieldName,fieldName)); //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label. } return fieldNames; } public void output() { system.debug('object'+selectedObject); String soqlQuery = 'select '+selectedField+', id, '+selectedField2+' from '+selectedObject; system.debug(soqlQuery); oList= Database.query(soqlQuery); system.debug(olist); } }
Please let me know were am I lacking. It would be really helpful.
Thanks and Regards,
Ramandeep
Thanks for your help. And somehow i managed to display the related data.
Thanks for your reply.
Sharing the working code. So, it may help others.
Visualforce Page:
Apex Class:
Regards,
Ramandeep
All Answers
Hope this helps,
apex:selectList will return the name of the filed, you need to use API name for query use.
Mark this as best answer if it helps.
Thanks for your help. And somehow i managed to display the related data.
Thanks for your reply.
Sharing the working code. So, it may help others.
Visualforce Page:
Apex Class:
Regards,
Ramandeep