+ Start a Discussion
chandrashekar Jangitichandrashekar Jangiti 

Hi,am sharing screen shot for need to dipsly related contacts of Account and selected record needs to be delete for this I need VF page code and controller

Hi,am sharing screen shot for need to dipsly related contacts of Account and selected record needs to be delete for this I need VF page code and controller 
User-added image
Best Answer chosen by chandrashekar Jangiti
Rajesh3699Rajesh3699
Hope this helps,

<apex:page standardController="Account" showHeader="true" extensions="ContactDeleteExtension">
     <apex:form id="formId">
         <apex:pageMessages id="error"></apex:pageMessages>
         
         <apex:pageBlock rendered="{! IF(noContacts == true, false, true)}">
             <apex:pageBlockButtons >
                 <apex:commandButton action="{!deleteContact}" value="Delete Contact" reRender="error,formId"/>
             </apex:pageBlockButtons>
             <apex:outputText value="No more contacts available to delete" rendered="{!noMoreContacts}" />
             <apex:pageBlockSection title="Select Contacts to delete" columns="1">
                 <apex:pageBlockTable value="{!conListWrapper}" var="c" cellpadding="2px" cellspacing="2px" width="100%">
                     <apex:column >
                         <apex:inputCheckbox value="{!c.isSelected}"/>                         
                     </apex:column>
                     <apex:column value="{!c.Con.Name}"/>
                     <apex:column value="{!c.Con.Email}"/>
                 </apex:pageBlockTable>
             </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
</apex:page>

Class

public class ContactDeleteExtension {

public List<Contact> conListForDelete {get;set;}
Public List<Contact> conListOfAccount {get;set;}
public Id pageId {get;set;}
public List<ContactWrapper> conListWrapper {get;set;}
public Boolean noContacts {get;set;}
public Boolean noMoreContacts {get;set;}

    public ContactDeleteExtension(ApexPages.StandardController controller) {
        conListForDelete = new list<Contact>();
        conListWrapper = new list<ContactWrapper>();
        conListOfAccount = new list<Contact>();
        noMoreContacts = false;
        
        //get the page Id and fetch the contacts 
        conListOfAccount = [select Id, Email, FirstName, Name,LastName from Contact where AccountId =: ApexPages.currentPage().getParameters().get('Id')];
        if(conListOfAccount.size() == 0){
            noContacts = true;
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Error, 'Selected account does not have any contact'));
        }
        else{
            for(Contact c : conListOfAccount){
                ContactWrapper cw = new ContactWrapper(false, c);
                conListWrapper.add(cw);
            }    
        }
    }
    
    Public List<ContactWrapper> updateConListWrapper(){
        conListOfAccount = [select Id, Email, FirstName, Name,LastName from Contact where AccountId =: ApexPages.currentPage().getParameters().get('Id')];
        if(conListOfAccount.size() > 0){
            for(Contact c : conListOfAccount){
                ContactWrapper cw = new ContactWrapper(false, c);
                conListWrapper.add(cw);
            }
        }
        else{
            noMoreContacts = true;
        }
        return conListWrapper;
    }
    
    Public pageReference deleteContact(){
        
        Integer count = 0;
        for(ContactWrapper c : conListWrapper){
            if(c.isSelected == true){
                count = count + 1;
                conListForDelete.add(c.Con);
            }                
        }
        
        if(count == 0){
            ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Error, 'Please select contact for delete'));
            return null;
        }
        if(conListForDelete.size() > 0){
            try{
                delete conListForDelete;
                ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Confirm, 'Selected contacts deleted successfully'));
                conListWrapper.clear();
                conListWrapper = updateConListWrapper();
            }catch(Exception e){
                ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Error, e.getMessage()));
                return null;
            }
        }
        return null;
    }
    
    Public Class ContactWrapper {
        Public Boolean isSelected {get;set;}
        Public Contact Con {get;set;}
        
        Public ContactWrapper(Boolean x, Contact c){
            isSelected = x;
            Con = c;
        }
        
    }

All Answers

Rajesh3699Rajesh3699
Hi 
You can use wrapper class for this, here is the example for the same.

https://developer.salesforce.com/page/Wrapper_Class

Let me know if you need the complete code for the same.

Thank You,
Rajesh.
 
chandrashekar Jangitichandrashekar Jangiti
Yes, I need total code
Rajesh3699Rajesh3699
Hope this helps,

<apex:page standardController="Account" showHeader="true" extensions="ContactDeleteExtension">
     <apex:form id="formId">
         <apex:pageMessages id="error"></apex:pageMessages>
         
         <apex:pageBlock rendered="{! IF(noContacts == true, false, true)}">
             <apex:pageBlockButtons >
                 <apex:commandButton action="{!deleteContact}" value="Delete Contact" reRender="error,formId"/>
             </apex:pageBlockButtons>
             <apex:outputText value="No more contacts available to delete" rendered="{!noMoreContacts}" />
             <apex:pageBlockSection title="Select Contacts to delete" columns="1">
                 <apex:pageBlockTable value="{!conListWrapper}" var="c" cellpadding="2px" cellspacing="2px" width="100%">
                     <apex:column >
                         <apex:inputCheckbox value="{!c.isSelected}"/>                         
                     </apex:column>
                     <apex:column value="{!c.Con.Name}"/>
                     <apex:column value="{!c.Con.Email}"/>
                 </apex:pageBlockTable>
             </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
</apex:page>

Class

public class ContactDeleteExtension {

public List<Contact> conListForDelete {get;set;}
Public List<Contact> conListOfAccount {get;set;}
public Id pageId {get;set;}
public List<ContactWrapper> conListWrapper {get;set;}
public Boolean noContacts {get;set;}
public Boolean noMoreContacts {get;set;}

    public ContactDeleteExtension(ApexPages.StandardController controller) {
        conListForDelete = new list<Contact>();
        conListWrapper = new list<ContactWrapper>();
        conListOfAccount = new list<Contact>();
        noMoreContacts = false;
        
        //get the page Id and fetch the contacts 
        conListOfAccount = [select Id, Email, FirstName, Name,LastName from Contact where AccountId =: ApexPages.currentPage().getParameters().get('Id')];
        if(conListOfAccount.size() == 0){
            noContacts = true;
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Error, 'Selected account does not have any contact'));
        }
        else{
            for(Contact c : conListOfAccount){
                ContactWrapper cw = new ContactWrapper(false, c);
                conListWrapper.add(cw);
            }    
        }
    }
    
    Public List<ContactWrapper> updateConListWrapper(){
        conListOfAccount = [select Id, Email, FirstName, Name,LastName from Contact where AccountId =: ApexPages.currentPage().getParameters().get('Id')];
        if(conListOfAccount.size() > 0){
            for(Contact c : conListOfAccount){
                ContactWrapper cw = new ContactWrapper(false, c);
                conListWrapper.add(cw);
            }
        }
        else{
            noMoreContacts = true;
        }
        return conListWrapper;
    }
    
    Public pageReference deleteContact(){
        
        Integer count = 0;
        for(ContactWrapper c : conListWrapper){
            if(c.isSelected == true){
                count = count + 1;
                conListForDelete.add(c.Con);
            }                
        }
        
        if(count == 0){
            ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Error, 'Please select contact for delete'));
            return null;
        }
        if(conListForDelete.size() > 0){
            try{
                delete conListForDelete;
                ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Confirm, 'Selected contacts deleted successfully'));
                conListWrapper.clear();
                conListWrapper = updateConListWrapper();
            }catch(Exception e){
                ApexPages.addMessage(new ApexPages.message(Apexpages.Severity.Error, e.getMessage()));
                return null;
            }
        }
        return null;
    }
    
    Public Class ContactWrapper {
        Public Boolean isSelected {get;set;}
        Public Contact Con {get;set;}
        
        Public ContactWrapper(Boolean x, Contact c){
            isSelected = x;
            Con = c;
        }
        
    }
This was selected as the best answer
chandrashekar Jangitichandrashekar Jangiti
Yes, it is working thank you so much...Rajesh...