0

I have requirement where I want to keep all value as selected in multi select picklist on visual force page.

as far as I know I can select one value as default selected on normal picklist but how to do that for multiselect picklist ?

Is there any standard approach to this ? Or Script to achieve that?

Here is my controller

 /****************
    Constructor
    ****************/
    public LeaseActionEdit_TempController(ApexPages.StandardController stdCont)
    {
      recordTypeId = Apexpages.currentPage().getParameters().get('RecordType');        
        isClone = Apexpages.currentPage().getParameters().get('clone');      
        isUnitRedemised = false;             
         if (Apexpages.currentPage().getParameters().containsKey('id'))
        {
            oppId = stdCont.getId();           
        } 
    }

      /****************
            Method to get value in opp
            ****************/

        public PageReference doLoad()
            {
                List<Opportunity> lsOpp = new List<Opportunity>();

                if (oppId != null && oppId != '')
                {
                    lsOpp = [SELECT Maintain__c  FROM Opportunity WHERE Id =: oppId
};

             opp = lsOpp[0];
}

/******************************
your code snippet on controller
************************************/
 public static List<String> getPickListValues(String TenantShallMaintain) {
        List<String> res = new List<String> ();
        for (Schema.PickListEntry ple: getPickListEntries(TenantShallMaintain))
            res.add(ple.getValue());
        return res;
    }
private static List<Schema.PickListEntry> getPickListEntries(String TenantShallMaintain) {
        return Schema.getGlobalDescribe().get(TenantShallMaintain.split('\\.')[0]).getDescribe().fields.getMap().get(TenantShallMaintain.split('\\.')[1]).getDescribe().getPickListValues();
    }
}
  • prepopulate the list bound to the multiselect selectList to all the choices (obtained from the schema) – cropredy Dec 29 '15 at 22:29
  • can you elaborate this? I did not understand. – Ricky Dec 29 '15 at 22:30
  • is this a roll-your-own multiselect list using apex:selectList ? or are you trying to default an apex:inputField ? – cropredy Dec 29 '15 at 22:46
  • this is default apex:inputfield. – Ricky Dec 30 '15 at 15:22
3

Let's start with two utility methods to return a list of strings corresponding to any Picklist-type Sobject.Fldname:

public static List<String> getPickListValues(String sObjNameDotFldName) {
        List<String> res = new List<String> ();
        for (Schema.PickListEntry ple: getPickListEntries(sObjNameDotFldName))
            res.add(ple.getValue());
        return res;
    }
private static List<Schema.PickListEntry> getPickListEntries(String sObjNameDotFldName) {
        return Schema.getGlobalDescribe().get(sObjNameDotFldName.split('\\.')[0]).getDescribe().fields.getMap().get(sObjNameDotFldName.split('\\.')[1]).getDescribe().getPickListValues();
    }

Then, if you want to prepopulate an Sobject field bound to an apex:inputField

mySobject.myField = String.join(getPickListValues('MyObject.MyField'),';');

Using your example, after you fetch opp in doLoad(),

opp.maintain__c = String.join(getPickListValues('Opportunity.Maintain__c');

=========================

If, on the other hand, you are doing a roll-your-own MSP using an apex:selectlist like the following:

<apex:selectList value="{!selectedVals}" multiselect="true">
  <apex:selectOptions value="{!availVals}"/>
</apex:selectList>

your Controller will need a getter for availVals and a getter/setter for selectedVals as follows:

public SelectOption[] getAvailVals() {
  SelectOption[] res = new List<SelectOption>(); 
  for (String s : getPickListValues('MyObject.MyField')) // e.g 'Opportunity.MyMspField__c'
      res.add(new SelectOption(s,s);
  return res;
}

public String[] selectedVals {
   get {
       if (this.selectedVals == null) // assumes some entry is required o.w. initialize in constructor
          this.selectedVals = new List<String>(getPickListValues('MyObject.MyField'));
       return this.selectedVals;
   }
   set;
 }

during the save actionmethod (or equivalent), you interrogate the selected values by referring to this.selectedVals

there may be typos in the above

  • Ok let me try implementing in my org. I will keep you posted with the outcome. I really appreciate your all efforts to write this for me. I will keep you posted on the same. Thanks – Ricky Dec 30 '15 at 14:00
  • hey one quick question I cant pass actuall sObjName.FldName as string here can I ? did you mean that or was this "sObjNameDotFldName" only a string? for this line on controller public static List<String> getPickListValues(String sObjNameDotFldName) { – Ricky Dec 30 '15 at 16:03
  • i updated the answer with an example – cropredy Dec 30 '15 at 17:06
  • I am not using select list I am only using standard <apex:inputtext field>. so I understand where should I add this line on visual force page mySobject.myField = String.join(getPickListValues(),';'); – Ricky Dec 30 '15 at 17:18
  • here is the input field on visual force force page I am using <apex:inputField value="{!opp.Maintain__c}" id="OppMaintain" /> how do I use your code snippet with the inputfield part? – Ricky Dec 30 '15 at 17:53

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.