I have made few changes to your code, and its workinh pretty fine. please have a look
Apex:
public with sharing class Picklist
{
public String err{get;set;}
//shortened variable names
public String sC1{get;set;}
public String sC2{get;set;}
public String sC3{get;set;}
public String sC4{get;set;}
public String sC5{get;set;}
public String sC6{get;set;}
public String sC7{get;set;}
public String sC8{get;set;}
public String sC9{get;set;}
public String sC10{get;set;}
//changed getCountriesOptions to cOs
public List<SelectOption> cOs;
public boolean disable{get; set;}
public Picklist(){
//populated selecOption List in constructor
cOs = new List<SelectOption>();
cOs.add(new SelectOption('','-None-'));
cOs.add(new SelectOption('INDIA','India'));
cOs.add(new SelectOption('USA','USA'));
cOs.add(new SelectOption('United Kingdom','UK'));
cOs.add(new SelectOption('Germany','Germany'));
cOs.add(new SelectOption('Ireland','Ireland'));
cOs.add(new SelectOption('bangladesh','bangladesh'));
cOs.add(new SelectOption('pakistan','pakistan'));
cOs.add(new SelectOption('srilanka','srilanka'));
cOs.add(new SelectOption('bhutan','bhutan'));
}
public List<SelectOption> getCountriesOptions() {
//simply return already populated list
return cOs;
}
//added new function, it is executed whenever selectList value is changed
public void removeCountry() {
Integer listSize = cOs.size();
//excluded index 0 in loop as it holds null value
for(Integer i=1;i<listSize;i++){
if(cOs[i].getValue()==sC1 || cOs[i].getValue()==sC2 || cOs[i].getValue()==sC3 || cOs[i].getValue()==sC4 || cOs[i].getValue()==sC5 || cOs[i].getValue()==sC6 || cOs[i].getValue()==sC7 || cOs[i].getValue()==sC8 || cOs[i].getValue()==sC9 || cOs[i].getValue()==sC10){
cos[i].setDisabled(true);
}
else if(cOs[i].getValue()!=sC1 && cOs[i].getValue()!=sC2 && cOs[i].getValue()!=sC3 && cOs[i].getValue()!=sC4 && cOs[i].getValue()!=sC5 && cOs[i].getValue()!=sC6 && cOs[i].getValue()!=sC7 && cOs[i].getValue()!=sC8 && cOs[i].getValue()!=sC9 && cOs[i].getValue()!=sC10){
cos[i].setDisabled(false);
}
}
}
}
i have 10 picklist with same values in it for eg opt1,opt2 so on......if in first picklist user choose opt1 then i should disable from the remaining 9 picklist field.
public with sharing class Picklist { public String selectedCountry1{get;set;} public String selectedCountry2{get;set;} public String selectedCountry3{get;set;} public String selectedCountry4{get;set;} public String selectedCountry5{get;set;} public String selectedCountry6{get;set;} public String selectedCountry7{get;set;} public String selectedCountry8{get;set;} public String selectedCountry9{get;set;} public String selectedCountry10{get;set;} public List<SelectOption> getCountriesOptions = new List<SelectOption>(); public boolean disable{get; set;} public List<SelectOption> getCountriesOptions() { List<SelectOption> countryOptions = new List<SelectOption>(); countryOptions.add(new SelectOption('','-None-')); countryOptions.add(new SelectOption('INDIA','India')); countryOptions.add(new SelectOption('USA','USA')); countryOptions.add(new SelectOption('United Kingdom','UK')); countryOptions.add(new SelectOption('Germany','Germany')); countryOptions.add(new SelectOption('Ireland','Ireland')); countryOptions.add(new SelectOption('bangladesh','bangladesh')); countryOptions.add(new SelectOption('pakistan','pakistan')); countryOptions.add(new SelectOption('srilanka','srilanka')); countryOptions.add(new SelectOption('bhutan','bhutan'));
Multiple annotations found at this line: - Save error: Unknown property 'DemoPicklist.PicklistOptions' - Save error: Unknown property 'DemoPicklist.strPicklist1'
Save error: Variable does not exist: getCountriesOptions
public class Picklist { public String strPicklist1 {get;set;} public String strPicklist2 {get;set;} public String strPicklist3 {get;set;} public String strPicklist4 {get;set;} public String strPicklist5 {get;set;} public String strPicklist6 {get;set;} public String strPicklist7 {get;set;} public String strPicklist8 {get;set;} public String strPicklist9 {get;set;} public String strPicklist10 {get;set;}
I am getting this error now. Visualforce Error System.NullPointerException: Attempt to de-reference a null object Class.Picklist.getPicklistOptions: line 21, column 1
I have made few changes to your code, and its workinh pretty fine. please have a look
Apex:
public with sharing class Picklist
{
public String err{get;set;}
//shortened variable names
public String sC1{get;set;}
public String sC2{get;set;}
public String sC3{get;set;}
public String sC4{get;set;}
public String sC5{get;set;}
public String sC6{get;set;}
public String sC7{get;set;}
public String sC8{get;set;}
public String sC9{get;set;}
public String sC10{get;set;}
//changed getCountriesOptions to cOs
public List<SelectOption> cOs;
public boolean disable{get; set;}
public Picklist(){
//populated selecOption List in constructor
cOs = new List<SelectOption>();
cOs.add(new SelectOption('','-None-'));
cOs.add(new SelectOption('INDIA','India'));
cOs.add(new SelectOption('USA','USA'));
cOs.add(new SelectOption('United Kingdom','UK'));
cOs.add(new SelectOption('Germany','Germany'));
cOs.add(new SelectOption('Ireland','Ireland'));
cOs.add(new SelectOption('bangladesh','bangladesh'));
cOs.add(new SelectOption('pakistan','pakistan'));
cOs.add(new SelectOption('srilanka','srilanka'));
cOs.add(new SelectOption('bhutan','bhutan'));
}
public List<SelectOption> getCountriesOptions() {
//simply return already populated list
return cOs;
}
//added new function, it is executed whenever selectList value is changed
public void removeCountry() {
Integer listSize = cOs.size();
//excluded index 0 in loop as it holds null value
for(Integer i=1;i<listSize;i++){
if(cOs[i].getValue()==sC1 || cOs[i].getValue()==sC2 || cOs[i].getValue()==sC3 || cOs[i].getValue()==sC4 || cOs[i].getValue()==sC5 || cOs[i].getValue()==sC6 || cOs[i].getValue()==sC7 || cOs[i].getValue()==sC8 || cOs[i].getValue()==sC9 || cOs[i].getValue()==sC10){
cos[i].setDisabled(true);
}
else if(cOs[i].getValue()!=sC1 && cOs[i].getValue()!=sC2 && cOs[i].getValue()!=sC3 && cOs[i].getValue()!=sC4 && cOs[i].getValue()!=sC5 && cOs[i].getValue()!=sC6 && cOs[i].getValue()!=sC7 && cOs[i].getValue()!=sC8 && cOs[i].getValue()!=sC9 && cOs[i].getValue()!=sC10){
cos[i].setDisabled(false);
}
}
}
}
I have made few changes to your code, and its workinh pretty fine.
please have a look
Apex:
VF Page
Hit like if it helped you in anyways. Select it as Best answer it it solved your problem. Cheers
All Answers
Do you mean.. if we select any vaule than that value should removed from the pick-list so that it should not selected again?
If possible share the requirment or code that will help me to give the solution.
Thanks,
controller
public with sharing class Picklist
{
public String selectedCountry1{get;set;}
public String selectedCountry2{get;set;}
public String selectedCountry3{get;set;}
public String selectedCountry4{get;set;}
public String selectedCountry5{get;set;}
public String selectedCountry6{get;set;}
public String selectedCountry7{get;set;}
public String selectedCountry8{get;set;}
public String selectedCountry9{get;set;}
public String selectedCountry10{get;set;}
public List<SelectOption> getCountriesOptions = new List<SelectOption>();
public boolean disable{get; set;}
public List<SelectOption> getCountriesOptions() {
List<SelectOption> countryOptions = new List<SelectOption>();
countryOptions.add(new SelectOption('','-None-'));
countryOptions.add(new SelectOption('INDIA','India'));
countryOptions.add(new SelectOption('USA','USA'));
countryOptions.add(new SelectOption('United Kingdom','UK'));
countryOptions.add(new SelectOption('Germany','Germany'));
countryOptions.add(new SelectOption('Ireland','Ireland'));
countryOptions.add(new SelectOption('bangladesh','bangladesh'));
countryOptions.add(new SelectOption('pakistan','pakistan'));
countryOptions.add(new SelectOption('srilanka','srilanka'));
countryOptions.add(new SelectOption('bhutan','bhutan'));
return countryOptions;
}
public Picklist()
{
}
}
VFPage
<apex:page controller="Picklist" sidebar="false">
<apex:form >
<apex:sectionHeader title="PickList Value Selection"/>
<apex:PageBlock >
<apex:pageBlockSection collapsible="false" >
<apex:selectList value="{!selectedCountry1}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry2}" multiselect="false" size="1">
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry3}" multiselect="false" size="1">
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry4}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry5}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry6}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry7}" multiselect="false" size="1">
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry8}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry9}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!selectedCountry10}" multiselect="false" size="1" >
<apex:selectOptions value="{!countriesOptions}" />
</apex:selectList>
</apex:pageBlockSection>
</apex:PageBlock>
</apex:form>
</apex:page>
There are two way to achieve the functionality one way using javascript and another way by apex.
I will help you how we can achiveve from apex controller.
Please check this code and let me know if this help.
Thanks
I have updated the code please check this
Thanks
controller:-Save error: Variable does not exist: getCountriesOptions
page:-Save error: Unknown property 'Picklist.strPicklist1'
- Save error: Unknown property
'DemoPicklist.PicklistOptions'
- Save error: Unknown property 'DemoPicklist.strPicklist1'
Save error: Variable does not exist: getCountriesOptions
or can you please share your controller and page code here
<apex:form >
<apex:sectionHeader title="PickList Value Selection"/>
<apex:PageBlock >
<apex:pageBlockSection collapsible="false" >
<apex:selectList value="{!strPicklist1}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist2}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist3}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist4}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist5}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist6}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist7}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist8}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist9}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
<apex:pageBlockSection collapsible="false">
<apex:selectList value="{!strPicklist10}" multiselect="false" size="1">
<apex:selectOptions value="{!PicklistOptions}" />
</apex:selectList>
</apex:pageBlockSection>
</apex:PageBlock>
</apex:form>
</apex:page>
{
public String strPicklist1 {get;set;}
public String strPicklist2 {get;set;}
public String strPicklist3 {get;set;}
public String strPicklist4 {get;set;}
public String strPicklist5 {get;set;}
public String strPicklist6 {get;set;}
public String strPicklist7 {get;set;}
public String strPicklist8 {get;set;}
public String strPicklist9 {get;set;}
public String strPicklist10 {get;set;}
public List<SelectOption> getPicklistOptions()
{
for(SelectOption a : getCountriesOptions)
{
if(strPicklist1.indexOf(a.getValue()) != -1 || strPicklist2.indexOf(a.getValue()) != -1 ||strPicklist3.indexOf(a.getValue()) != -1 ||
strPicklist4.indexOf(a.getValue()) != -1 || strPicklist5.indexOf(a.getValue()) != -1 || strPicklist6.indexOf(a.getValue()) != -1 ||
strPicklist7.indexOf(a.getValue()) != -1 || strPicklist8.indexOf(a.getValue()) != -1 || strPicklist9.indexOf(a.getValue()) != -1 ||
strPicklist10.indexOf(a.getValue()) != -1 ) continue;
options.put(new selectOption(a.getValue(), a.getLabel()));
}
return options;
}
public List<SelectOption> getCountriesOptions() {
List<SelectOption> countryOptions = new List<SelectOption>();
countryOptions.add(new SelectOption('','-None-'));
countryOptions.add(new SelectOption('INDIA','India'));
countryOptions.add(new SelectOption('USA','USA'));
countryOptions.add(new SelectOption('United Kingdom','UK'));
countryOptions.add(new SelectOption('Germany','Germany'));
countryOptions.add(new SelectOption('Ireland','Ireland'));
countryOptions.add(new SelectOption('bangladesh','bangladesh'));
countryOptions.add(new SelectOption('pakistan','pakistan'));
countryOptions.add(new SelectOption('srilanka','srilanka'));
countryOptions.add(new SelectOption('bhutan','bhutan'));
return countryOptions;
}
}
Visualforce Error
System.NullPointerException: Attempt to de-reference a null object
Class.Picklist.getPicklistOptions: line 21, column 1
This is going to help you surely.
Thanks
Please let me know if this works for you.. also choose as best answer if that help you
I have made few changes to your code, and its workinh pretty fine.
please have a look
Apex:
VF Page
Hit like if it helped you in anyways. Select it as Best answer it it solved your problem. Cheers