2

How to splice a list in apex?

 for(String stagename : pickListValuesList)
        {
            if(stagename == 'Closed Won' || stagename == 'Closed Lost')
            {
                pickListValuesList.splice(i);
            }
        }
2
  • Exactly what you want to do? Do you want to insert a list after a particular index in another list? Jan 27, 2020 at 5:45
  • What is ur requirement?Splice is not a function in apex
    – user75427
    Jan 27, 2020 at 6:10

1 Answer 1

6

It seems that you want to remove elements "Closed Won" and "Closed Lost" from the list. You can do it by the below code.

pickListValuesList.remove(pickListValuesList.indexOf('Closed Won'));
pickListValuesList.remove(pickListValuesList.indexOf('Closed Lost'));

Since "splice" is not a function for apex lists. You can find all the functions supported by apex list here - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

2
  • 5
    +1, although if the values may be duplicated or simply not present, a while loop would apply here: Integer index; while((index = picklistValuesList.indexOf('Closed Won')) > -1) picklistValuesList.remove(index); while((index = picklistValuesList.indexOf('Closed Lost')) > -1) picklistValuesList.remove(index);
    – sfdcfox
    Jan 27, 2020 at 6:05
  • @sfdcfox If that's a factor I'd probably just write out a loop over all the elements, e.g. for(integer i = picklistValuesList.size(); i --> 0;){if(picklistValuesList[i] == 'Closed Won' || picklistValuesList[i] == 'Closed Lost'){picklistValuesList.remove(i);}} Jan 27, 2020 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .