1

So this is i donw so far, In this pick list there is two value like active,new. I want to set this both option as pre select by default.

Visua force page

    <tr>
        <td style="font-weight:bold;">Property Status<br/>

<apex:selectList id="status" size="8" style=" max-height: 250px !important;overflow-y: scroll !important;"  multiselect="true" value="{!test7}" onchange="updatebnk7(this.value)"   >
        <apex:selectOption itemValue="no" itemLabel=" "/>
        <apex:selectOptions value="{!status}"  />
         <apex:actionSupport event="onchange" reRender="results" />
 </apex:selectList>
<apex:actionFunction name="updte7" action="{!check7}">
                   <apex:param assignTo="{!test7}" value="val" name="current"/>
  </apex:actionFunction>
        </td>
      </tr>

Apex controller

public list<SelectOption> status{
    get {

        Schema.DescribeFieldResult field = REOHQ__REOHQ_Property__c.REOHQ__REOHQ_Property_Status__c.getDescribe();

              List<Schema.PicklistEntry> ple = field.getPicklistValues();

        for( Schema.PicklistEntry f : ple){

        if(n != 1){

          optn4.add(new selectOption(f.getValue(),f.getLabel()));
         }
}


      return optn4;          
    }
    set;
  }
  • Is updatebnk7 a method in your controller? You have this set for a multi-select list, yet one value is "Active" and the other is "Deactive". Can you actually have "both" conditions at the same time without a conflict? Off-hand, that doesn't seem wise or appropriate considering the values. In general though, the answer would be to set them in your controller extension or custom controller that you're using. – crmprogdev Apr 5 '16 at 16:45
  • please take a look at updated code – Salesfor_dev Apr 5 '16 at 17:03
0

Edited based on Question edit


Since you're describing a picklist in a getter, you cannot reliably pre-populate the values in your list. Also, your code is (possibly) buggy because it may cause the opt4 list to grow each time the getter is called. Instead, consider populating this value just the first time:

public list<SelectOption> status{
    get {
        if(optn4 == null) {
            opt4n = new List<SelectOption>();
            for( Schema.PicklistEntry f : REOHQ__REOHQ_Property__c
                                          .REOHQ__REOHQ_Property_Status__c
                                          .getDescribe()
                                          .getPicklistValues()){
                optn4.add(new selectOption(f.getValue(),f.getLabel()));
            }
        }
        return optn4;
     }
}

At this point, though, you'd still need to iterate over the values initially in your constructor:

public MyController() {
    test4 = new List<String>();
    for(SelectOption option: status) {
        test4.add(option.getValue());
    }
    // More code here
}

Don't use an actionFunction this way, because you're just asking for trouble. First of all, "value" won't be populated the way you expect, because it's a multi-select picklist. Secondly, the values in test7 will automatically be written back into the value any time you perform an action; bindings are two-way by default when using an input element of any type.

  • check updated code, Hope it make sense – Salesfor_dev Apr 5 '16 at 17:02
  • @Salesfor_dev I've updated my answer for you. – sfdcfox Apr 5 '16 at 17:17
  • I this where you have declare "Active" and "New" picklist value to pre select by default? – Salesfor_dev Apr 5 '16 at 17:47
0

What you've posted is the code that generates your select option list for status. OnChange, you call an ActionFunction in your JS or Controller for check7 that sends the selected values to that method. What does that method look like? More importantly, someplace in your constructor, there's code that calls your status method.

Once your select options have been returned to your constructor, you could call another method that would look something like this:

 public list<string>selectedStatuses;

 public list<string> SetselectedStatuses(list<SelectOption> status) {


        selectedStatus = new list<string>();
        for (SelectOption selOption : status)
        {   

            if (selOption.getValue() == 'active' || 'new')
            {                   
                selectedStatuses.add(selOption.getValue());

            }


        }

        // set selectedStatuses;
        return selectedStatuses;
   }

The return sets the selectedStatuses to the defaults in the values you've set.

Later, use a similar method to determine what's been selected using a getSelectedstatus method where you iterate on the SelectOptions. Compare the results with what's in selectedStatuses (your default values). If they're different, use the new results. If they're not, use the default values.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.