function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Visualforce page with a list of standard and custom objects!!!

Hi,

   I need to build a visualforce page that does the following:

1. It has a controlling  picklist that displays all the custom and standard objects of the org.

2. It has a dependent multi select picklist of all the fields of object selected in controlling picklist mentioned above.

 

    Please help me with the code.I 've found few examples , but they use sObjects (list) . I don't want to go through that approach. Is there a better way?

 

sfdcfoxsfdcfox

Not sure what you're asking: ... "but they use sObjects (list)." I've actually written something like this previously. It goes like this:

 

<apex:page controller="mycontroller">
	<apex:form id="form">
		<apex:pageBlock>
			<apex:pageBlockSection columns="1">
				<apex:pageBlockSectionItem>
					<apex:outputLabel>Objects</apex:outputLabel>
					<apex:selectList size="1" value="{!currentobject}">
						<apex:selectOptions value="{!objects}"/>
						<apex:actionSupport event="onchange" action="{!loadfields}" reRender="form"/>
					</apex:selectList>
				</apex:pageBlockSectionItem>
				<apex:pageBlockSectionItem>
					<apex:outputLabel>Field</apex:outputLabel>
					<apex:selectList size="1" value="{!currentfield}">
						<apex:selectOptions value="{!fields}"/>
					</apex:selectList>
				</apex:pageBlockSectionItem>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>

 

public class mycontroller {
	public string currentobject { get; set; }
	public string currentfield { get; set; }
	
	public selectoption[] getobjects() {
		selectoption[] objects = new selectoption[0];
		map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
		for(string objectname:describe.keyset()) {
			objects.add(new selectoption(objectname,describe.get(objectname).getdescribe().getname()));
		}
		return objects;
	}
	
	public selectoption[] getfields() {
		selectoption[] fields = new selectoption[0];
		map<string,schema.sobjecttype> describe = schema.getglobaldescribe();
		if(describe.containskey(currentobject)) {
			map<string,schema.sobjectfield> fieldmap = describe.get(currentobject).getdescribe().fields.getmap();
			for(string fieldname:fieldmap.keyset()) {
				fields.add(new selectoption(fieldname,fieldmap.get(fieldname).getdescribe().getlabel()));
			}
		}
		return fields;
	}
}

This is just an example, but it should work out of the box with minimal fuss.

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

This helped a lot, Thanks a lot for your time and effort. But on seletion of the object in picklist I am geyying this error message given below:


Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.ApexELResolver$VisualforceArrayList.

   Can you please help me with that?

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

and there is no !loadfields action in class So I replaced it with GetFields method of yours!!!

Hargobind_SinghHargobind_Singh
Thats the reason you are getting this error. Action method functions can only return PageReference, so you would need to write an Action Function. Here is some information to get you started:   https://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_controller_setter_methods.htm