Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
If you click on Account (parent) name, related contact will display and you can edit and delete the contacts (child). Also, you can create new related contact.
public class AccConAddDeletec {
public String message {get;set;}
public List<WrapperClass> sub {get;set;}
public Boolean check_box {get;set;}
public List<Contact> del {get;set;}
public string recId{get;set;}
public list<Account> Acclst{get;set;}
public boolean displayPopup {get; set;}
public String accid {get;set;}
public Account con {get;set;}
public Contact contact {get;set;}
public List<Contact> acnt;
public AccConAddDeletec(){
Acclst = [select Id,Name from Account LIMIT 10];
sub=null;
contact = new Contact();
con = new Account();
acnt = new List<Contact>();
}
public void closePopup() {
displayPopup = false;
}
public void showPopup() {
contact = new Contact();
displayPopup = true;
con = [SELECT Id, Name, Phone, (SELECT firstName, lastName FROM Contacts) FROM Account WHERE Id = :accid];
}
public void updateContact(){
contact.AccountId = con.Id;
acnt.add(contact);
System.debug('acnt-> ' + acnt);
upsert acnt;
closePopup();
}
public void setupContacts() {
sub = new List<WrapperClass>();
for(Contact cr : [SELECT Id, FirstName, LastName from Contact where AccountId=:recId]){
sub.add(new WrapperClass (cr, false));
}
}
public PageReference quickUpdate(){
try{
List<Contact> accts = new List<Contact>();
for(WrapperClass w : sub){
accts.add(w.cs);
}
UPDATE accts;
return ApexPages.CurrentPage();
}catch(Exception e){
message='Data Base error during saving...';
ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, message));
return null;
}
}
public PageReference delete_now(){
del=new list<Contact>();
for(WrapperClass cc: sub){
if(cc.check_box){
del.add(cc.cs);
}
}
delete del;
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
public class WrapperClass {
public Contact cs {get; set;}
public Boolean check_box {get; set;}
public WrapperClass(Contact cs, Boolean check_box){
this.cs = cs;
this.check_box = check_box;
}
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
If you click on Account (parent) name, related contact will display and you can edit and delete the contacts (child). Also, you can create new related contact.
public class AccConAddDeletec {
public String message {get;set;}
public List<WrapperClass> sub {get;set;}
public Boolean check_box {get;set;}
public List<Contact> del {get;set;}
public string recId{get;set;}
public list<Account> Acclst{get;set;}
public boolean displayPopup {get; set;}
public String accid {get;set;}
public Account con {get;set;}
public Contact contact {get;set;}
public List<Contact> acnt;
public AccConAddDeletec(){
Acclst = [select Id,Name from Account LIMIT 10];
sub=null;
contact = new Contact();
con = new Account();
acnt = new List<Contact>();
}
public void closePopup() {
displayPopup = false;
}
public void showPopup() {
contact = new Contact();
displayPopup = true;
con = [SELECT Id, Name, Phone, (SELECT firstName, lastName FROM Contacts) FROM Account WHERE Id = :accid];
}
public void updateContact(){
contact.AccountId = con.Id;
acnt.add(contact);
System.debug('acnt-> ' + acnt);
upsert acnt;
closePopup();
}
public void setupContacts() {
sub = new List<WrapperClass>();
for(Contact cr : [SELECT Id, FirstName, LastName from Contact where AccountId=:recId]){
sub.add(new WrapperClass (cr, false));
}
}
public PageReference quickUpdate(){
try{
List<Contact> accts = new List<Contact>();
for(WrapperClass w : sub){
accts.add(w.cs);
}
UPDATE accts;
return ApexPages.CurrentPage();
}catch(Exception e){
message='Data Base error during saving...';
ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR, message));
return null;
}
}
public PageReference delete_now(){
del=new list<Contact>();
for(WrapperClass cc: sub){
if(cc.check_box){
del.add(cc.cs);
}
}
delete del;
PageReference pr = new PageReference(System.currentPageReference().getURL());
pr.setRedirect(true);
return pr;
}
public class WrapperClass {
public Contact cs {get; set;}
public Boolean check_box {get; set;}
public WrapperClass(Contact cs, Boolean check_box){
this.cs = cs;
this.check_box = check_box;
}
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
I trust you are doing very well.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
If you click on Account (parent) name, related contact will display and you can edit and delete the contacts (child). Also, you can create new related contact.
Visualforce:
Controller:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
All Answers
I trust you are doing very well.
Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
If you click on Account (parent) name, related contact will display and you can edit and delete the contacts (child). Also, you can create new related contact.
Visualforce:
Controller:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.
Thanks and Regards,
Khan Anas
Thanks for your reply.I will try this and let you know if it is working
thanka
Instead of commandbutton i using commandlink to delete item.
Now the page is refreshing .Item needs to be removed from view list and database.
Thanks