function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anupamaaj1.3905395811417727E12anupamaaj1.3905395811417727E12 

Apex : how to remove selected items from selectlist ?

Hi,

For adding the item in a selectlist we do the following

for(String f :  FieldLabels)
        {
             selectedFieldsList.add(new selectOption(f,f));        
       
        }
and this works perfect.

Same way how to remove from the select list ? I dtried with the following things and got the errors

E.g : 1 : for(String f : selectedFields)
        {
             selectedFieldsList.remove(f);           
        }
ERROR :  Method does not exist or incorrect signature: [LIST<System.SelectOption>].remove(String)

for(integer i=0;i<selectedFields.Size();i++)
        {
                      selectedFieldsList.remove(selectedFields.Size());
        }
ERROR : LIST INDEX OUT OF BONDS

for(String f : selectedFields)
        {
         
           selectedFieldsList.remove(0); 
          
        }

here the items are deleted but , if i select 5 random items to delete, it is removing first 5 items not the selected one.
harsha__charsha__c
Try below:

If you know the index of the value, you like to delete, then the below one works
for(integer i=0;i<selectedFields.Size();i++)
{
        if(i == indexToBeremoved)
               selectedFieldsList.remove(i);
}

If you just know the value to be removed and not sure about it's index, then you can try the below.

Suppose, you have the element to be removed from the Select Options in a string variable named "selectedValue"

for(
integer i=0;i<selectedFields.Size();i++)
{
       
if(selectedFields[i].getValue() == selectedValue)
               selectedFieldsList.remove(i);

}

Hope this helps..!

Regards,
- Harsha
nansi kela 21nansi kela 21
Hi Harsha,

Plz provide me full code of controller.. 
where i need to add these lines of codes..

Regards,
Nansi