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
SalesforceAddictSalesforceAddict 

how can i delete records for particular contact on click by del hyperlink in action field

VF page
<apex:page controller="contactEditDelete" tabStyle="Account">
    <apex:form >
    <apex:pageBlock title="Account Detail">
        <apex:pageBlockSection >
        <apex:outputField value="{!acc.Name}"/>
        <apex:outputField value="{!acc.Phone}"/>
        <apex:outputField value="{!acc.Fax}"/>
        <apex:outputField value="{!acc.AccountNumber}"/>
        </apex:pageBlockSection>
        
       <apex:pageBlockSection title="Contact" collapsible="false" columns="1">
           <apex:pageBlockTable value="{!acts}" var="conts">
               <apex:column headerValue="Action">
               <apex:commandLink value="Edit |" action="{!editRedirect}">
               <apex:param value="{!conts.Id}" name="selectedContactId" assignTo="{!selectedContact}"/>
               </apex:commandLink>
               <apex:commandLink value=" Del"/>
               </apex:column>
               <apex:column value="{!conts.Name}"/>
               <apex:column value="{!conts.Title}"/>
               <apex:column value="{!conts.Email}"/>
           </apex:pageBlockTable>
       </apex:pageBlockSection>
        
     </apex:pageBlock>
    </apex:form>
</apex:page>
    
   Controller
public class contactEditDelete{

public Account acc {get;set;}
public String selectedContact {get;set;}
public String currentRecordId {get;set;}
public String parameterValue {get;set;}


    public contactEditDelete(){
       Id id  = ApexPages.CurrentPage().getparameters().get('id');
       acc  =  (id==null)?new Account():[select  Name,Phone,fax,AccountNumber From Account where Id =:id];
    
        
    }
    public List<Contact> getacts(){
          currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
          List<Contact> acts = [select id,Name,Title,Email,Phone from Contact where AccountId =: currentRecordId ];
          parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');
          return acts;
    }
         
    public PageReference editRedirect(){
    selectedContact =apexpages.currentPage().getParameters().get('selectedContactId');
    PageReference pg=new PageReference('/'+selectedContact+'/e');
    return pg;
        
    }
          
}
Best Answer chosen by SalesforceAddict
PawanKumarPawanKumar
Hi Sachin,

You just need add param tag for delete command link as well how you have added for edit. then it will work.

VF
---------------
<apex:page controller="contactEditDelete" tabStyle="Account">
    <apex:form >
    <apex:pageBlock title="Account Detail">
        <apex:pageBlockSection >
        <apex:outputField value="{!acc.Name}"/>
        <apex:outputField value="{!acc.Phone}"/>
        <apex:outputField value="{!acc.Fax}"/>
        <apex:outputField value="{!acc.AccountNumber}"/>
        </apex:pageBlockSection>
        
       <apex:pageBlockSection title="Contact" collapsible="false" columns="1" id="pgBId">
           <apex:pageBlockTable value="{!acts}" var="conts">
               <apex:column headerValue="Action">
               <apex:commandLink value="Edit |" action="{!editRedirect}">
               <apex:param value="{!conts.Id}" name="selectedContactId" assignTo="{!selectedContact}"/>
               </apex:commandLink>
               <apex:commandLink value="Del" action="{!deleteSelectedContact}">
                   <apex:param value="{!conts.id}" assignTo="{!delContId}" name="delContId"/>
               </apex:commandLink>
               </apex:column>
               <apex:column value="{!conts.Name}"/>
               <apex:column value="{!conts.Title}"/>
               <apex:column value="{!conts.Email}"/>
           </apex:pageBlockTable>
       </apex:pageBlockSection>
        
     </apex:pageBlock>
    </apex:form>
</apex:page>

----------------------
Controller
-----------------------

public class contactEditDelete{

public Account acc {get;set;}
public String selectedContact {get;set;}
public String delContId{get;set;}
public String currentRecordId {get;set;}
public String parameterValue {get;set;}


    public contactEditDelete(){
          Id id  = ApexPages.CurrentPage().getparameters().get('id');
       acc  =  (id==null)?new Account():[select  Name,Phone,fax,AccountNumber From Account where Id =:id];
        
    }
    public List<Contact> getacts(){
          currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
          List<Contact> acts = [select id,Name,Title,Email,Phone from Contact where AccountId =: currentRecordId ];
          parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');
          return acts;
    }
         
    public PageReference editRedirect(){
    selectedContact =apexpages.currentPage().getParameters().get('selectedContactId');
    PageReference pg=new PageReference('/'+selectedContact+'/e');
    return pg;
        
    }
    
             // added action method for delete
    public PageReference deleteSelectedContact(){
     String delSelContId= ApexPages.CurrentPage().getparameters().get('delContId');
       
    if(delSelContId!=null && String.isNotEmpty(delSelContId)){
    Contact delContact = [Select Id from Contact where Id=:delSelContId];
               delete delContact;
        }
        
    return null;
    }

    
          
}

Please mark best if it helps you. Thanks in advance.

Regards,
Pawan Kumar

All Answers

PawanKumarPawanKumar
Hi Sachin,

You just need add param tag for delete command link as well how you have added for edit. then it will work.

VF
---------------
<apex:page controller="contactEditDelete" tabStyle="Account">
    <apex:form >
    <apex:pageBlock title="Account Detail">
        <apex:pageBlockSection >
        <apex:outputField value="{!acc.Name}"/>
        <apex:outputField value="{!acc.Phone}"/>
        <apex:outputField value="{!acc.Fax}"/>
        <apex:outputField value="{!acc.AccountNumber}"/>
        </apex:pageBlockSection>
        
       <apex:pageBlockSection title="Contact" collapsible="false" columns="1" id="pgBId">
           <apex:pageBlockTable value="{!acts}" var="conts">
               <apex:column headerValue="Action">
               <apex:commandLink value="Edit |" action="{!editRedirect}">
               <apex:param value="{!conts.Id}" name="selectedContactId" assignTo="{!selectedContact}"/>
               </apex:commandLink>
               <apex:commandLink value="Del" action="{!deleteSelectedContact}">
                   <apex:param value="{!conts.id}" assignTo="{!delContId}" name="delContId"/>
               </apex:commandLink>
               </apex:column>
               <apex:column value="{!conts.Name}"/>
               <apex:column value="{!conts.Title}"/>
               <apex:column value="{!conts.Email}"/>
           </apex:pageBlockTable>
       </apex:pageBlockSection>
        
     </apex:pageBlock>
    </apex:form>
</apex:page>

----------------------
Controller
-----------------------

public class contactEditDelete{

public Account acc {get;set;}
public String selectedContact {get;set;}
public String delContId{get;set;}
public String currentRecordId {get;set;}
public String parameterValue {get;set;}


    public contactEditDelete(){
          Id id  = ApexPages.CurrentPage().getparameters().get('id');
       acc  =  (id==null)?new Account():[select  Name,Phone,fax,AccountNumber From Account where Id =:id];
        
    }
    public List<Contact> getacts(){
          currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
          List<Contact> acts = [select id,Name,Title,Email,Phone from Contact where AccountId =: currentRecordId ];
          parameterValue = ApexPages.CurrentPage().getparameters().get('nameParam');
          return acts;
    }
         
    public PageReference editRedirect(){
    selectedContact =apexpages.currentPage().getParameters().get('selectedContactId');
    PageReference pg=new PageReference('/'+selectedContact+'/e');
    return pg;
        
    }
    
             // added action method for delete
    public PageReference deleteSelectedContact(){
     String delSelContId= ApexPages.CurrentPage().getparameters().get('delContId');
       
    if(delSelContId!=null && String.isNotEmpty(delSelContId)){
    Contact delContact = [Select Id from Contact where Id=:delSelContId];
               delete delContact;
        }
        
    return null;
    }

    
          
}

Please mark best if it helps you. Thanks in advance.

Regards,
Pawan Kumar
This was selected as the best answer
SalesforceAddictSalesforceAddict
thnx allot sir its working