You need to sign in to do that
Don't have an account?
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;
}
}
<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;
}
}
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
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