+ Start a Discussion
Ankit Kumar 169Ankit Kumar 169 

Want to create a Vf page Which displays Accounts Name and related contact[Picklist]

Create a Vf page Which displays Accounts Name and related contact[Picklist] in a table format and Having a button in the last Column ""Delete"".It will delete the account and all the contacts related to the account."
Best Answer chosen by Ankit Kumar 169
Rajesh3699Rajesh3699
User-added image

All Answers

Fazurulla GagangapalliFazurulla Gagangapalli
Hi Ankit,

try this code for pick list 

VF page:
<apex:page controller="AccountController1_picklistrendering">
   <apex:form >
      <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" reRender="a"/>
             </apex:selectList>
          
             <br/><br/>
           
           Related Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
             <apex:selectList value="{!selectedConId}" size="1"  id="a">
                <apex:selectOptions value="{!ContactNames}" />
             </apex:selectList>
       </apex:pageBlock>               
    </apex:form>
</apex:page>

Controller:
public with sharing class AccountController1_picklistrendering {
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
      
          public List<SelectOption> getAccountNames() {
                  List<SelectOption> accOptions= new List<SelectOption>();
                  accOptions.add( new SelectOption('','--Select--'));
                  for( Account acc : [select Id,name from Account ] ) {
                          accOptions.add( new SelectOption(acc.Id,acc.name));
                  }
                 return accOptions;
           }
         
           public List<SelectOption> getContactNames() {
                  System.debug('Entered ContactNames account id...........'+selectedAccId );
                  List<SelectOption> conOptions= new List<SelectOption>();
                  List<SelectOption> options = new List<SelectOption>();
                    if(selectedAccId != null)
                    {     
                       for( contact con : [select Id,name,accountid from contact where accountid=:selectedAccId ] ) {
                          conOptions.add( new SelectOption(con.Id,con.name));
                       }
                    }                  
                    else
                    {
                        conOptions.add( new SelectOption('--None--','--None--'));
                    }
                 return conOptions;
           }
    }


Hope so this helps you...!

Please mark this answer a Solution, if you found this answer as helpful.
Ankit Kumar 169Ankit Kumar 169
Hi,

I want the vf page like this,only particular contacts would be shown as related to particular Account.
In contact picklist it is shown all contacts. Please find the code below and resolve it .
Thnaks in Advance.
Image of requirement

Controller :::

public class PickController {
    public List<Account> accList {get;set;}
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
    public PickController(){
                              accList=[select Id,name,AccountNumber from Account];

    }
 public List<SelectOption> getContactNames() {
                  System.debug('Entered ContactNames account id...........'+accList);
                  List<SelectOption> conOptions= new List<SelectOption>();
                  List<SelectOption> options = new List<SelectOption>();
                    if(accList != null)
                    {     
                       for( contact con : [select Id,name,accountid,account.name from contact where accountid=:accList ] ) {
                          conOptions.add( new SelectOption(con.Id,con.name));
                       }
                    }                  
                    else
                    {
                        conOptions.add( new SelectOption('--None--','--None--'));
                    }
                 return conOptions;
           }
    public PageReference delete1(){
        return null;
    }
}

VF Page ::

<apex:page controller="PickController">
    <apex:form>
    
        <apex:pageBlock title="Data Table">

        <apex:dataTable value="{!accList}" var="a" styleClass="outBorder" width="1000px">

            <apex:column styleClass="inBorder">

                <apex:facet name="header">Account Name</apex:facet>

                <apex:outputText >{!a.Name}</apex:outputText>

            </apex:column>      
      <apex:column styleClass="inBorder" >
    
                   <apex:facet name="header">Contacts</apex:facet>
             <apex:selectList value="{!selectedConId}" size="1"  id="a">
                <apex:selectOptions value="{!ContactNames}" />
             </apex:selectList>
        
            </apex:column>
            <apex:column styleClass="inBorder">

                <apex:facet name="header">Action Button</apex:facet>

                <apex:commandButton value="Delete" action="{!delete1}"></apex:commandButton>
            </apex:column>     
            

        </apex:dataTable>
   
        
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
Rajesh3699Rajesh3699
Hello Ankit,

There are  few things in my code that you need to add

1. Delete Function - I have just written a blank method.
2. Ensure that the user selects the check and clicks delete button
3. There is only one delete button in the page.
4. Page only displays that account, which has the contacts

Let me know if you need more help. 
Thanks
Rajesh [ you can connect to me over linkedIn]
https://in.linkedin.com/in/rajesh-adiga-p-b3611ba7
 
Public Class DeleteSelectedContact{

    Public list<AccountContactWrapper> accountContactList {get;set;}
    Public Map<Id, list<Contact>> accContactMap = new Map<Id, list<Contact>>();
    Public Map<Id, Account> accMap = new map<Id, Account>();
    
    Public DeleteSelectedContact (){
        
        accountContactList = new list<AccountContactWrapper>();
        list<Account> accList = [select Id, name from Account ];
        if(accList.size() > 0){
            
            
            Set<Id> accId = new Set <Id>();
            for(Account a : accList){
                
                accId.add(a.Id);
            }
            
            List<Contact> ContactList = [select Id, Name, Account.Name from Contact where AccountId IN: accId];
            system.debug('the contact size is' + ContactList.size());
            if(ContactList.size() > 0){
                Set<Id> acId = new set<Id>();
                for(COntact c : ContactList){
                    acId.add(c.AccountId);
                }
                
                accList = [select Id, Name from Account where Id IN: acId];
                for(Account a : accList){
                    accMap.put(a.Id, a);
                }
                
                
                
                for(Contact c: ContactList)
                {
                    Boolean x = accContactMap.containsKey(c.AccountId);
                    if(!x)
                    {
                        list<Contact> cList = new list<Contact>();
                        cList.add(c);
                        accContactMap.put(c.AccountId, cList);
                    }
                    else
                    {
                        list<Contact> cListUpdate = new list<Contact>();
                        cListUpdate = accContactMap.get(c.AccountId);
                        cListUpdate.add(c);
                        accContactMap.remove(c.AccountId);
                        accContactMap.put(c.AccountId, cListUpdate);
                    }
                }
                system.debug('the map is' + accContactMap);
                
        }
    }
    
    //For every account in account list there should be a wrapper record
    for(Account a : accMap.Values())
    {    
        list<Contact> conAccList = new List<Contact>();
        conAccList = accContactMap.get(a.Id);
        AccountContactWrapper acWrap = new AccountContactWrapper(a, conAccList);
        accountContactList.add(acWrap);
    }
    
    system.debug('the wrap value is' + accountContactList);
 }  
 
     Public void deleteContact(){
         system.debug('delete selected contact');
     } 

    Public Class AccountContactWrapper{
        Public Account act {get;set;}
        Public list<Contact> contactList {get;set;}
        Public Boolean isSelected {get;set;}
        Public String conName {get;set;}
        Public List<SelectOption> optionsCon {get;set;}
        
        
        Public AccountContactWrapper(Account a, list<Contact> ct){
            optionsCon = new list<SelectOption>();
            contactList = new list<Contact>();
            act = a;
            contactList = ct;
            isSelected = false;
            conName = '--None--';
            optionsCon.add(new selectOption('--None--', '--None--'));
            If(ct.size() > 0){
                for(Contact c: ct){
                    optionsCon.add(new selectOption(c.Name, c.Name));
                }
            }
            
        }
        
        Public list<SelectOption> getOptionsCon(){
            return optionsCon;
        }

}
}
 
<apex:page tabStyle="Contact" controller="DeleteSelectedContact">
    <apex:form >
        <apex:pageBlock title="Please click checkbox to delete contact" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Delete Contact" action="{!deleteContact}" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!accountContactList}" var="acWrap" width="100%" cellpadding="2px" cellspacing="2px">
                <apex:column >
                    <apex:inputCheckBox value="{!acWrap.isSelected}" />                 
                </apex:column>
                <apex:column value="{!acWrap.act.Name}" />
                <apex:column >
                    <apex:selectList value="{!acWrap.conName}" multiselect="false" size="1">
                        <apex:selectOptions value="{!acWrap.optionsCon}" />
                    </apex:selectList>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>



 
Rajesh3699Rajesh3699
User-added image
This was selected as the best answer
Ankit Kumar 169Ankit Kumar 169
Thanks Rajesh for your help.