0

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.

2 Answers 2

6

There is just a small logic issue with your code. you are updating variable I before IF statement(adding the index number to deleteIndex list), that results into setting incorrect index. the variable I should be incremented after adding the index number to deleteIndex list. try below code-

    /*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){    
       if(deleteRecord.thisContact.id == removeThis){
           deleteIndex.add(I);
           system.debug(removeThis+'..................This record is deleted');
       }
       I++;  
     }
   for(Integer index  : deleteIndex){
       selectedContactValues.remove(index);
     }
   }
2
  • Anup, could you also explain your answer ? Providing this info will help us all learn from this question a great deal more. Commented Apr 3, 2015 at 8:05
  • @SamuelDeRycke, Explanation added in answer.
    – Anup
    Commented Apr 3, 2015 at 17:34
0

You can try below :

Class

public void removeContacts(){ 
       Integer rowToRemove = Integer.ValueOf(ApexPages.currentPage().getParameters().get('rowToRemove')); 
       selectedContactValues.remove(rowToRemove);      
   }   

Page

        <apex:variable value="{!0}" var="rowNum"/> 
        <apex:pageBlockTable value="{!selectedContactValues}" var="this">
             <apex:column headerValue="Select All">           
                   <apex:commandLink value="Remove" action="{!removeContacts}" rerender="selectedContactDetails">                           
                            <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>                                                         
                   </apex:commandLink>
                   <apex:variable var="rowNum" value="{!rowNum + 1}"/>

You must log in to answer this question.

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