1

I have a requirement to create a VisualForce Page that creates multiple records and deletes the record when I click on delete button, but I'm getting a VisualForce error ..Please Help....

    <apex:actionFunction reRender="frm" name="act" status="process" action="{!cdelete}">
          <apex:param assignTo="{!idx}" name="idx" value=""/>
    </apex:actionFunction>

    <apex:pageBlock >
        <table  cellpadding='5' cellspacing='0' width='500px'>

                <apex:variable value="{!i}"  var="index"/ >
                <apex:repeat value="{!contacts}" var="m">   
                <tr>               
                    <td><apex:inputfield value="{!m.Firstname}"/></td>

                    <td><apex:inputfield value="{!m.Lastname}" required="false" /></td>

                    <td><apex:inputfield value="{!m.Email}"/></td>

                    <td><apex:inputfield value="{!m.Phone}"/></td>

                    <td><apex:commandButton value="Delete" action="act('{!index}')" /></td>
                    <apex:variable var="index" value="{!index+1}" /> 
               </tr>
               </apex:repeat>

           <tr>
           <td colspan="5" align="center"><apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Add" action="{!Add}"/></td>
          </tr>
       </table>
    </apex:pageBlock>
</apex:form>

//controller
public class ContactController{

public List<Contact> Contacts{get;set;}
public Integer i{get;set;}
public Integer idx{get;set;}

public ContactController(){


    i=0;
    Contacts = new List<Contact>();
    Contacts.add(new Contact());
}

public void save() {
    list <contact> con1=new list <contact>(); 
       for(Contact obj:contacts){
           if(obj.Lastname!=null)
           {     
                    con1.add(obj); 
           }  
        } 
      insert con1;

}

public void Add(){ 

    Contacts.add(new contact());
}

public void cdelete(){

    Contacts.Remove(idx);
 }
}

2 Answers 2

2

Replace the "action" in delete commandbutton to "onclick". This should resolve your issue

<td><apex:commandButton value="Delete" onclick="act('{!index}')" /></td>
                <apex:variable var="index" value="{!index+1}" />

Thanks & Regards
Saranya

Try this code for the page -

            <apex:variable value="{!0}"  var="index"/ >
            <apex:repeat value="{!contacts}" var="m">   
            <tr>               
                <td><apex:inputfield value="{!m.Firstname}"/></td>

                <td><apex:inputfield value="{!m.Lastname}" required="false" /></td>

                <td><apex:inputfield value="{!m.Email}"/></td>

                <td><apex:inputfield value="{!m.Phone}"/></td>

                <td><apex:commandButton value="Delete" onclick="act('{!index}')" rerender="false"/></td>

           </tr>
           <apex:variable var="index" value="{!index+1}" />
           </apex:repeat>

       <tr>
       <td colspan="5" align="center"><apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Add" action="{!Add}" rerender="pb"/></td>
      </tr>
   </table>
</apex:pageBlock>

2
  • I replace "action" in delete commandbutton to "onclick" .. I m not getting Error but my record is not deleted from the list ...
    – anamika
    Oct 18, 2014 at 18:17
  • Try the code pasted in my answer and check if it works for you. I tried and it worked.
    – Saranya
    Oct 19, 2014 at 17:26
0

Do two thinks :: 1 . You are using the action function then you must use the Ajax method instead of button Action, replace the action with onclick.

  1. You can update in Apex controller when you removing the record from the only list not in salesforce. If you need to delete the record as well as salesforce then you check the contact record id is available then you must delete the record from the salesforce

Regards Satya

0

You must log in to answer this question.

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