I am need to remove a Contact record in the List on click of the commandLink. To remove the record I am calling the function below :
removeContacts
/*Method to remove contacts*/
public void removeContacts(){
ID removeThis = ApexPages.currentPage().getParameters().get('selectID');
List<Integer> deleteIndex = new List<Integer>();
Integer I = 0;
deleteIndex.clear();
for(selectedRecord deleteRecord : selectedContactValues){
I++;
if(deleteRecord.thisContact.id == removeThis){
deleteIndex.add(I);
system.debug(removeThis+'..................This record is deleted');
}
}
for(Integer index : deleteIndex){
selectedContactValues.remove(index);
}
}
The Visualforce Page block is as below :
<apex:outputText value="Selected Contacts"/>
<apex:pageBlock id="selectedContactDetails">
<apex:pageBlockTable value="{!selectedContactValues}" var="this">
<apex:column headerValue="Select All">
<apex:commandLink value="Remove" action="{!removeContacts}" rerender="selectedContactDetails">
<apex:param name="selectID" value="{!this.thisContact.ID}"/>
</apex:commandLink>
</apex:column>
<apex:column headerValue="First Name" value="{!this.thisContact.FirstName}"/>
<apex:column headerValue="Last Name" value="{!this.thisContact.LastName}"/>
<apex:column headerValue="Email" value="{!this.thisContact.Email}"/>
<apex:column headerValue="Function" value="{!this.thisContact.Domain__c}"/>
<apex:column headerValue="City" value="{!this.thisContact.City__c}"/>
<apex:column headerValue="Country" value="{!this.thisContact.Country__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
The page is displaying the records on the page load initially. When I delete the first record also it does not give any error. But when I delete the second or any other record (except first record) it gives me the below error :
System.ListException: List index out of bounds: 1 Error is in expression '{!removeContacts}' in page wrapperdemo_con: Class.liveYourPotentialEmailer.removeContacts: line 80, column 1
or
System.ListException: List index out of bounds: 4 Error is in expression '{!removeContacts}' in page wrapperdemo_con: Class.liveYourPotentialEmailer.removeContacts: line 78, column 1
or
System.ListException: List index out of bounds: 6 Error is in expression '{!removeContacts}' in page wrapperdemo_con: Class.liveYourPotentialEmailer.removeContacts: line 78, column 1
Error is shown depending on the index of the record that I try to delete.