0

I'm having some trouble understanding how to do this with my code. I feel like im really close since im populating all the available values to the picklist but its not pre selecting the values set on the record (account) or inserting them.

I need some help understanding how to change my vf page and apex controller to not only show the pre selected values on the account record for the multi select picklist but also be able to insert them back after changing when I click save.

As you can see below im using the bootstrap multi-select styling and script. I have a feeling part of the solution is going to need jQuery?

Any help is greatly appreciated. Feel free to load up my code given to see where im at yourself...It will work in a fresh org just create a multi select called LOC_Offered__c on the account object with at least 5 values.

Here is what I have so far VF Page:

    <apex:page controller="multiController" standardStylesheets="false" showHeader="false" sidebar="false">
<head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" ></script>
           <link href="https://cdnjs.cloudflare.com/ajax/libs/multi-select/0.9.12/css/multi-select.css" rel="stylesheet" />
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/css/bootstrap-select.min.css"/>    

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.7.5/js/bootstrap-select.min.js"></script> 

    </head>
     <apex:form > 
     <apex:pageBlock mode="edit">
    <div class="container">

 <div class="tab-content2">
        <div class="col-md-3"></div>

<div class="col-md-6" id="account">
   <div class="form-area">  

        <br style="clear:both"/>
                    <h3 style="margin-bottom: 25px; text-align: center;">Account Information</h3>
                    <div class="form-group">

                    </div>

                    <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="glyphicon glyphicon-user" aria-hidden="true"> Name:</i></span>
                        <apex:inputField styleClass="form-control" value="{!Account.Name}"/> </div>   
                    </div>

<div class="form-group">
    <apex:actionFunction name="passSelectedStagesToController" action="{!retrieveLocForSelectedStages}" rerender="hiddenDiv">               
       <apex:param assignTo="{!selectedStages}" name="selectedStages" value=""/>  
  </apex:actionFunction>  
        <div class="row">
            <div class="form-group col-xs-12">
                <label class="sr-only" for="firstname"></label>
                 <span class="input-group-addon"><i aria-hidden="true" class="glyphicon glyphicon-check"> Levels of Care Offered:</i></span>
<select id="accountMultiSelect" data-style="btn-default" class="selectpicker form-control" multiple="multiple" data-max-options="20">
 <apex:repeat value="{!locStages}" var="stage">  
                 <option value="{!stage}">{!stage}</option>  
            </apex:repeat>
    </select>
            </div>
        </div>
</div>

<h4 style="margin-bottom: 25px; text-align: center;">Contact</h4>

                    <div class="form-group">
                       <div class="input-group">
              <span class="input-group-addon"><span class="glyphicon glyphicon-earphone" aria-hidden="true"> Phone:</span></span>
                           <apex:inputField styleClass="form-control" value="{!Account.Phone}"/>
            </div> 
                    </div> 

<div class="form-group">
                       <div class="input-group">
              <span class="input-group-addon"><span class="glyphicon glyphicon-earphone" aria-hidden="true"> Fax:</span></span>
                           <apex:inputField styleClass="form-control" value="{!Account.Fax}"/>
            </div> 
                    </div> 

 <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="glyphicon glyphicon-home" aria-hidden="true"> Street:</i></span>
                        <apex:inputField styleClass="form-control" value="{!Account.BillingStreet}"/>

                    </div>   

                    </div>

                <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="glyphicon glyphicon-home" aria-hidden="true"> City:</i></span>

                        <apex:inputField styleClass="form-control" value="{!Account.BillingCity}"/>

                    </div>   
                    </div>

                <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="glyphicon glyphicon-home" aria-hidden="true"> State:</i></span>

                        <apex:inputField styleClass="form-control" value="{!Account.BillingState}"/>

                    </div>   

                    </div>

                <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="glyphicon glyphicon-home" aria-hidden="true"> Zip:</i></span>

                        <apex:inputField styleClass="form-control" value="{!Account.BillingPostalCode}"/>

                    </div>   

                    </div>

                <center> 
                <apex:commandButton styleClass="btn btn-primary pull-right" value="Save" action="{!save2}"/>
                </center>

    </div><!--form area2-->
     </div><!--col-->
         </div><!--tab content-->

</div>
        </apex:pageBlock> </apex:form>
</apex:page>

And my Controller:

public with sharing class multiController {


    public Account account { get; private set; }
    public String selectedStages {get; set;}

    public multiController() {

           account = [
                SELECT Id, Name, Fax, Phone,Owner.Email,Owner.Name,Owner.Phone,Owner.Title,LOC_Offered__c,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry
                FROM Account 
                WHERE Name = 'GenePoint' LIMIT 1
            ];

    }

    public List<String> locStages {  
       get {  
            if(locStages == null){  
                 locStages = new List<String>();  
                 Schema.Describefieldresult stageNameField  = Schema.Account.LOC_Offered__c.getDescribe();  
                 List<Schema.Picklistentry> stages = stageNameField.getPickListValues();  
                 for(Schema.Picklistentry stage : stages){  
                      locStages.add(stage.getValue());  
                 }  
            }  
            return locStages;  
       }  
       private set;  
  }  

    public void retrieveLocForSelectedStages(){  
       Set<String> stages = (Set<String>)JSON.deserialize(this.selectedStages, Set<String>.class);  
  } 


    public PageReference save2() {
        try {
            update(account);
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        //  After Save, navigate to the default view page:
        //return null;
        PageReference reRend = new PageReference('/apex/multiexample');
        reRend.setRedirect(true);
        return reRend;
    }

}
2
  • 1
    Why can't you just create the account on the controller, fill the picklist field with your desired values, and on the Visualforce use the apex:inputField tag? Jul 22, 2018 at 19:04
  • That would not look nice. I don't want two giant boxes where im juggling chosen/available values between
    – Lavasocks
    Jul 23, 2018 at 14:59

0

You must log in to answer this question.

Browse other questions tagged .