I am attempting to create the select list of options for a multi-select picklist by using a VisualForce page and a controller.
The scenario is this : I have an object for Vendors(Vendor_Profile__c). It has a child object of Products(Product_Profile__c). I have a separate object called Policy(Policy_Profile__c). When I create a new Policy I would like to choose which products are affected by the new Policy. The user would choose a Vendor it is relevant to in a lookup field (Vendor__c) and then I want a query to produce the select options of a multi-select picklist (Products_Affected2__c) with all of the Products available from that Vendor.
Currently, my controller is producing an 'Invalid type : Products_Affected2__c' (the multi-value field). This is the controller :
public class MycontrollerSFDC
{
String policyid;
public Policy_Profile__c policy{get;set;}
public string relatedVendor{get;set;}
public List<String> names{get;set;} // As multi select, that's why changes from string to list
public string vendorid{get;set;}
public string selectedvendorid{get;set;}
public Name policynew{get;set;}
public SelectOption[] SelectedProducts { get; set; }
public MycontrollerSFDC(apexpages.standardcontroller controller)
{
policy = new Policy_Profile__c();
policyid=system.currentpagereference().getparameters().get('id');
if(policyid!=null)
{
policy=[select Vendor__c,Products_Affected2__c from Policy_Profile__c where id =:policyid];
relatedVendor=Policy.Vendor__c;
}
}
public pageReference execute()
{
vendorid=policy.Vendor__c;
System.debug('########'+vendorid);
return null;
}
public list<selectoption> getitems()
{
List<selectoption> options= new list<selectoption>();
SelectedProducts = new List<SelectOption>();
if(vendorid != null)
{
Vendor_Profile__c a =[select name, ( Select Id, Name from Vendor_Products__r) from Vendor_Profile__c where id =:vendorid]; //Query the Policy Profiles
for(Vendor_Product__c s : a.Vendor_Products__r)
{
options.add(new SelectOption(s.Id,s.name)); //Added the Id
}
}
else
options.add(new SelectOption('','None'));
return options;
}
public void save()
{
System.debug('********************************' + names);
// Added for multi select
String products = '';
for( String str : names )
{
products += products + ';';
}
products = products.subString( 0, products.length() - 1 );
policy.Products_Affected2__c=products;
// Added for multi select
insert policy;
policy=new Policy_Profile__c ();
System.debug('********************************' + policy);
}
}
Here is the Visual Force page :
<apex:page standardcontroller="Policy_Profile__c" extensions="MycontrollerSFDC">
<apex:sectionheader title="New Policy"/>
<apex:form >
<apex:pageblock mode="edit" title="{!$ObjectType.Policy_Profile__c.label} Edit">
<apex:pageblockbuttons >
<apex:commandbutton value="Save New Policy" action="{!Save}"/>
<apex:commandbutton value="Cancel" action="{!Cancel}"/>
</apex:pageblockbuttons>
<apex:outputpanel id="ProductsDetails">
<apex:pageblocksection title="Vendors and Products" columns="2">
<apex:inputfield value="{!policy.Vendor__c}" required="false" >
<apex:actionSupport event="onchange" action="{!execute}"
rerender="ProductsDetails,SelectedProducts" immediate="false" />
</apex:inputfield>
<apex:inputfield value="{!policy.Products_Affected2__c}" required="false"/>
</apex:inputfield>
</apex:pageblocksection>
<apex:pageBlockSection title="Subsidiaries" id="SelectedProducts">
<apex:pageBlockSectionItem >
<apex:outputLabel >Press Ctrl to select multiple subs:</apex:outputLabel>
<apex:selectList value="{!names}" multiselect="true" size="6">
<apex:actionSupport event="onclick" action="{!execute}"
reRender="field"/>
<apex:selectOptions value="{!items}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Products Selected:</apex:outputLabel>
<apex:outputtext value="{!names}" id="field"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:outputpanel>
</apex:pageblock>
</apex:form>
</apex:page>
Could I please get some input on this problem ? There may be other problems with the code as well. I don't know because I have never been able to save and run it.
Any help would be greatly appreciated.
Products_Affected2__c
is a multiselect picklist (MSP), then values assigned to it must be separated with semi-colons (save()
method) – cropredy Aug 11 '15 at 18:18names
bound to in the VF page? – cropredy Aug 11 '15 at 19:25