You need to sign in to do that
Don't have an account?
How to find the index of a list item
I see that the List object has a remove(index) method but how do I find the index of a specific item in the list?
I thought I could loop through the list and if certain conditions were true then I could remove that specific item using the list iterator but I get an error message stating
I thought I could loop through the list and if certain conditions were true then I could remove that specific item using the list iterator but I get an error message stating
Method does not exist or incorrect signature: [LIST<Case>].remove(Id)Here is the segment of code that is failing;
List<Case> RecChildren = new List<Case>(); RecChildren=[SELECT id,status, casenumber,parentid,subject, recordtypeid FROM Case WHERE parentid IN:RecIds]; for(Case c:RecChildren){ if(c.RecordTypeId!=pdc){ RecChildren.remove(c.id); } else { if(c.Status=='Closed'){ c.status='Open'; } } } update RecChildren;
There is no method either in list or set class in salesforce to retireve the index of an element and the remove method requires an integer as a parameter which remove the entry at that particular index. If you need to know the index of an element in list or set, you need to loop through all of the collection elements and find it out.
However in your case, i would recommend you to use maps instead of list as below:
Now, by end of loop you will have all the cases with desired values left in the map, you can collect them back using values() method and udpate them.
Hope it helps:
Thanks,
balaji
All Answers
There is no method either in list or set class in salesforce to retireve the index of an element and the remove method requires an integer as a parameter which remove the entry at that particular index. If you need to know the index of an element in list or set, you need to loop through all of the collection elements and find it out.
However in your case, i would recommend you to use maps instead of list as below:
Now, by end of loop you will have all the cases with desired values left in the map, you can collect them back using values() method and udpate them.
Hope it helps:
Thanks,
balaji