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();

            policy=[select Vendor__c,Products_Affected2__c from Policy_Profile__c  where id =:policyid];

    public pageReference execute()
        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
            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 );
        // 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: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 value="{!policy.Products_Affected2__c}" required="false"/>

    <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}"             
            <apex:selectOptions value="{!items}"/>

     <apex:pageBlockSectionItem >  
        <apex:outputLabel >Products Selected:</apex:outputLabel> 
        <apex:outputtext value="{!names}" id="field"/> 


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.

  • if 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:18
  • crop - I have a save( ) method in there. Am I using it incorrectly ? – Zoom_v Aug 11 '15 at 18:45
  • what is names bound to in the VF page? – cropredy Aug 11 '15 at 19:25
  • It is linked to the items selected. I've added the VF page now. – Zoom_v Aug 11 '15 at 19:31
  • cool - use String.join to assemble a list of strings separated by a delimiter semi-colon - one statement – cropredy Aug 11 '15 at 20:14


You are not querying for the Related Policy_Profiles__r in the vendor query

Vendor__c a =[select name from Vendor__c where id =:vendorid]

thus this will never run

for(Products_Affected2__c s : a.Policy_Profiles__r)

Maybe the query should be

Vendor__c a =[select name, (Select Products_Affected2__c From Policy_Profiles__r) from Vendor__c where id =:vendorid]

in addition the for above is invalid because Products_Affected2__c is a field on Policy_Profiles so it should be

for(Policy_Profile__c s : a.Policy_Profiles__r)
  • Eric - thank you very much for your help. I don't understand what you mean with "You are not queryingn for the Related Policy_Profiles__r in the vendor query". What do you mean by that ? Do you know what the proper query would be in that case ? Keep in mind, I have a Vendor__c field in the Policy_Profile object as well as a Vendor__c object. I don't know if that has anything to do with what you're saying, but I just wanted to mention that. – Zoom_v Aug 11 '15 at 18:10
  • The way you were doing it makes it seem like the Policy_Profiles__r should be a subQuery in the vendor query you were doing via relationships. If it is just a field then why would you need a loop? – Eric Aug 11 '15 at 18:14
  • I changed the query by putting in the Product_Profile__c object. I'm trying to figure out a way to find the vendor from the Vendor__c field of the Policy_Profile__c object - and then somehow capture the name of all of the Product_Profile__c child records listed below it. But I'm at a loss for now. – Zoom_v Aug 11 '15 at 18:30
  • Ok, I've got it working now. I put the correct code up on the original post. Thanks for all of your help. – Zoom_v Aug 11 '15 at 20:12
  • @Zoom_v - Great, please mark as accepted if this answer helped solve it for you so that it does not keep surfacing. – Eric Aug 11 '15 at 22:40

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.