You need to sign in to do that
Don't have an account?
donob
Using selectList with a Map of objects and their fields
I'm trying to use a Map to select object fields. I want to be able to select certain fields and then make queries on those fields.
I use describe to get the fields from selected objects and then put them into a map. I want to be able to select certain fields and populate them into a correspong map.
I am trying to use the component found here http://blogs.developerforce.com/developer-relations/2012/06/a-multiselect-picklist-visualforce-component.html but it's not working for my map. I've tried using a normal selectList and I can display the fields (though it's ugly atm) but after selecting it doesn't populate to the correspoding map. I've tried using a List as well and got the same results.
My visualforce page here.
<apex:page standardController="Custom_Object__c" extensions="customController">
<apex:pageBlock >
<apex:form >
<apex:commandButton value="Select Objects" action="{!getObjectFields}" rerender="picklists"/>
<apex:commandbutton value="Get Selected Fields" action="{!printOptions}" rerender="out"/>
<apex:pageBlockSection >
<c:MultiselectPicklist leftLabel="Objects" leftOptions="{!SupportedObjects}" rightLabel="Objects" rightoptions="{!SelectedObjects}" size="14" width="150px"/> <!--This works-->
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
<apex:pageBlock >
<apex:form >
<apex:pageBlockSection >
<apex:outputPanel id="picklists">
<apex:repeat value="{!objectFieldMap}" var="key">
<apex:selectList value="{!selectedFieldsMap[key]}" multiselect="true"><--I can see the fields, but can't populate-->
<apex:selectOptions value="{!objectFieldMap[key]}"></apex:selectOptions>
</apex:selectList>
<!--<c:MultiselectPicklist leftLabel="Objects" leftOptions="{!objectFieldMap[key]}" rightLabel="Objects" rightoptions="{!selectedFieldsMap[key]}" size="14" width="150px"/> This doesn't work -->
</apex:repeat>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>
My Controller here
public class customController{
public List<selectOption> supportedObjects {get; set;}//Used for component to get objects -- this works
public List<selectOption> SelectedObjects {get; set;}//Used for component to get objects -- this works
public Map<String,SelectOption[]> objectFieldMap{get;set;}//<-- This contains all fields for an object
public Map<String,String[]> selectedFieldsMap{get;set;}//<-- This should receive the selected fields
public List<List<SelectOption>> fieldSelections{get;set;}//<-- This contains all fields for an object when I tried using a list
public List<List<String>> selectedFields{get;set;}//<--This should receive the selected fields when I tried using a list
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
public LookbookCreationController(ApexPages.StandardController stdController){
SelectedObjects = new List<SelectOption>();
fieldSelections= new List<List<SelectOption>>();
init();
}
private void init(){
supportedObjects = new List<selectoption>();
Schema.DescribeFieldResult fieldResult = Custom_Object__c.Object__c.getDescribe();//Mulitselect picklist on custom object that has a list of objects to use
for( Schema.PicklistEntry e : fieldResult.getPicklistValues() ){
supportedObjects.add( new SelectOption( e.getLabel(), e.getValue() ) );
}
}
public PageReference printOptions(){
System.debug( selectedFieldsMap );//Using this to see if the map is populated.
return null;
}
public void getObjectFields(){
selectedFields = new List<List<String>>();
objectFieldMap = new Map<String,List<SelectOption>>();
selectedFieldsMap = new Map<String,List<String>>();
for(SelectOption o : SelectedObjects){
//Describe stuff to get all fields for the selected Objects. This part works
List<SelectOption> options = new List<SelectOption>();
Schema.SObjectType systemObjectType = gd.get(o.getValue());
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap();
for( Schema.SObjectField fieldAPI : M.values() ) {
options.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;
}
objectFieldMap.put( o.getValue(), options );
selectedFieldsMap.put( o.getValue(), new List<String>() );
fieldSelections.add( options );
selectedFields.add( new List<String>() );
}
}
When I use the Map or List, things display but selecting doesn't populate. Using the component I get the following error Cannot convert the value of '{!leftOptions}' to the expected type. After selecting the objects and trying to display the component.
I use describe to get the fields from selected objects and then put them into a map. I want to be able to select certain fields and populate them into a correspong map.
I am trying to use the component found here http://blogs.developerforce.com/developer-relations/2012/06/a-multiselect-picklist-visualforce-component.html but it's not working for my map. I've tried using a normal selectList and I can display the fields (though it's ugly atm) but after selecting it doesn't populate to the correspoding map. I've tried using a List as well and got the same results.
My visualforce page here.
<apex:page standardController="Custom_Object__c" extensions="customController">
<apex:pageBlock >
<apex:form >
<apex:commandButton value="Select Objects" action="{!getObjectFields}" rerender="picklists"/>
<apex:commandbutton value="Get Selected Fields" action="{!printOptions}" rerender="out"/>
<apex:pageBlockSection >
<c:MultiselectPicklist leftLabel="Objects" leftOptions="{!SupportedObjects}" rightLabel="Objects" rightoptions="{!SelectedObjects}" size="14" width="150px"/> <!--This works-->
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
<apex:pageBlock >
<apex:form >
<apex:pageBlockSection >
<apex:outputPanel id="picklists">
<apex:repeat value="{!objectFieldMap}" var="key">
<apex:selectList value="{!selectedFieldsMap[key]}" multiselect="true"><--I can see the fields, but can't populate-->
<apex:selectOptions value="{!objectFieldMap[key]}"></apex:selectOptions>
</apex:selectList>
<!--<c:MultiselectPicklist leftLabel="Objects" leftOptions="{!objectFieldMap[key]}" rightLabel="Objects" rightoptions="{!selectedFieldsMap[key]}" size="14" width="150px"/> This doesn't work -->
</apex:repeat>
</apex:outputPanel>
</apex:pageBlockSection>
</apex:form>
</apex:pageBlock>
</apex:page>
My Controller here
public class customController{
public List<selectOption> supportedObjects {get; set;}//Used for component to get objects -- this works
public List<selectOption> SelectedObjects {get; set;}//Used for component to get objects -- this works
public Map<String,SelectOption[]> objectFieldMap{get;set;}//<-- This contains all fields for an object
public Map<String,String[]> selectedFieldsMap{get;set;}//<-- This should receive the selected fields
public List<List<SelectOption>> fieldSelections{get;set;}//<-- This contains all fields for an object when I tried using a list
public List<List<String>> selectedFields{get;set;}//<--This should receive the selected fields when I tried using a list
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
public LookbookCreationController(ApexPages.StandardController stdController){
SelectedObjects = new List<SelectOption>();
fieldSelections= new List<List<SelectOption>>();
init();
}
private void init(){
supportedObjects = new List<selectoption>();
Schema.DescribeFieldResult fieldResult = Custom_Object__c.Object__c.getDescribe();//Mulitselect picklist on custom object that has a list of objects to use
for( Schema.PicklistEntry e : fieldResult.getPicklistValues() ){
supportedObjects.add( new SelectOption( e.getLabel(), e.getValue() ) );
}
}
public PageReference printOptions(){
System.debug( selectedFieldsMap );//Using this to see if the map is populated.
return null;
}
public void getObjectFields(){
selectedFields = new List<List<String>>();
objectFieldMap = new Map<String,List<SelectOption>>();
selectedFieldsMap = new Map<String,List<String>>();
for(SelectOption o : SelectedObjects){
//Describe stuff to get all fields for the selected Objects. This part works
List<SelectOption> options = new List<SelectOption>();
Schema.SObjectType systemObjectType = gd.get(o.getValue());
Schema.DescribeSObjectResult r = systemObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap();
for( Schema.SObjectField fieldAPI : M.values() ) {
options.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;
}
objectFieldMap.put( o.getValue(), options );
selectedFieldsMap.put( o.getValue(), new List<String>() );
fieldSelections.add( options );
selectedFields.add( new List<String>() );
}
}
When I use the Map or List, things display but selecting doesn't populate. Using the component I get the following error Cannot convert the value of '{!leftOptions}' to the expected type. After selecting the objects and trying to display the component.
This issue comes up due to the Gap between Custom Component and the Controller,
Read the below thread for similar discussion,
http://salesforce.stackexchange.com/questions/18396/custom-picklist-elements
And See the Wiki for the code to Connect - Communicate Custom Component and Controller,
http://wiki.developerforce.com/page/Controller_Component_Communication
Regards,
Ashish