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
bouscalbouscal 

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
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;





 
Best Answer chosen by bouscal
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@bouscal:

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:
 
Map<id,case> RecChildrenMap=new Map<id,case>([SELECT id,status, casenumber,parentid,subject, recordtypeid FROM Case WHERE parentid IN:RecIds]);
		 for(Case c:RecChildrenMap.values()){

            if(c.RecordTypeId!=pdc){
                RecChildrenMap.remove(c.id);
            } else {
                if(c.Status=='Closed'){
					Case UpdatedCase=RecChildrenMap.get(c.id);
					UpdatedCase.status='Open';
                    RecChildrenMap.put(c.id,UpdatedCase);
                }
            }
        }




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

Balaji Chowdary GarapatiBalaji Chowdary Garapati
@bouscal:

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:
 
Map<id,case> RecChildrenMap=new Map<id,case>([SELECT id,status, casenumber,parentid,subject, recordtypeid FROM Case WHERE parentid IN:RecIds]);
		 for(Case c:RecChildrenMap.values()){

            if(c.RecordTypeId!=pdc){
                RecChildrenMap.remove(c.id);
            } else {
                if(c.Status=='Closed'){
					Case UpdatedCase=RecChildrenMap.get(c.id);
					UpdatedCase.status='Open';
                    RecChildrenMap.put(c.id,UpdatedCase);
                }
            }
        }




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
This was selected as the best answer
bouscalbouscal
Thank you Balaji!