2

How to splice a list in apex?

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

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

  • 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 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);}} – IllusiveBrian Jan 27 at 16:00

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.