+ Start a Discussion
Sandeep YadavSandeep Yadav 

I want to delete contact records individually in visualforcepage

Am, using the concept of Wrapper class. I want to delete recently created contact record from pageBlockTable.
I am facing Unknown property 'SingleRecordDelete.ContactWrp.id' Error
Here is What I did--

VF page

<apex:page controller="SingleRecordDelete">
  <apex:form >
     <apex:pageBlock title="Contacts">
          <apex:pageBlockSection title="New Contact" columns="2" id="fresh">
              <apex:inputText label="FirstName" value="{!fname}" />
              <apex:inputText label="LastName" value="{!lname}"/>
              <apex:commandButton action="{!save}" value="Save" reRender="lab,fresh"/>
          </apex:pageBlockSection> 
         
          <apex:pageBlockSection title="ContactList" id="lab">
              <apex:pageBlockTable value="{!crp}" var="cy" id="update">
                  <apex:column headerValue="FirstName">
                      <apex:outputLabel value="{!cy.cn.firstname}"/>
                  </apex:column>
                  <apex:column headerValue="LastName">
                      <apex:outputLabel value="{!cy.cn.lastname}"/>
                  </apex:column>
                  <apex:column >
                         <apex:commandLink value="Delete" action="{!doDelete}" style="color:Red" reRender="lab">
                             <apex:param name="did" value="{!cy.id}" assignTo="{!did}"/>
                         </apex:commandLink>
                  </apex:column>
                  
              </apex:pageBlockTable>
                  
          </apex:pageBlockSection> 
               
     </apex:pageBlock>
  </apex:form>
</apex:page>

ApexClass
public with sharing class SingleRecordDelete {

    public String fname {get;set;}
    public String lname {get;set;}
    public String did {get;set;}
    
    public List<ContactWrp> crp {get;set;}
    
    public SingleRecordDelete(){
        crp = new List<ContactWrp>();
        
    }
    
    public void save(){
      Contact c = new Contact();
      c.firstname = fname;
      c.lastname = lname;
      insert c;
      crp.add(new ContactWrp(c));
      fname='';
      lname='';
    }
    
    public void doDelete()
    {
        List<Contact> contactToBeDelete = new List<Contact>();
        Contact cod = [select id from Contact where id =: did];
        contactToBeDelete.add(cod);
        delete contactToBeDelete;
    }
    
     public class ContactWrp{
    
      public Contact cn {get;set;}
      
        public ContactWrp(Contact cs){
            this.cn = cs;                   
        }
    }
}
Best Answer chosen by Sandeep Yadav
GauravendraGauravendra
Hi Sandeep,

Everything in your code seems fine except where you are passing the id of contact to be deleted in apex:param.
Inspite of value="{!cy.id}", it should be value="{!cy.cn.id}".
<apex:commandLink value="Delete" action="{!doDelete}" style="color:Red" reRender="lab">
                  <apex:param name="did" value="{!cy.cn.id}" assignTo="{!did}"/>
</apex:commandLink>

 

All Answers

GauravendraGauravendra
Hi Sandeep,

Everything in your code seems fine except where you are passing the id of contact to be deleted in apex:param.
Inspite of value="{!cy.id}", it should be value="{!cy.cn.id}".
<apex:commandLink value="Delete" action="{!doDelete}" style="color:Red" reRender="lab">
                  <apex:param name="did" value="{!cy.cn.id}" assignTo="{!did}"/>
</apex:commandLink>

 
This was selected as the best answer
Sandeep YadavSandeep Yadav
Thanks Gauravendra for pointing me.