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
Gayathri Adoni04Gayathri Adoni04 

Display a related contact information onclicking of radio button

HI all,

Can anyone help me,My requirement is i want to display 3 columns 1st column only radio button,2nd column account name and 3rd column account number.when i click that radio button beside that account name, i should get related contact information of that  particular account
Best Answer chosen by Gayathri Adoni04
ManojjenaManojjena
Hi G04,
You can try with below code which will display related contacts in a page block below .Try to modify the place where you want to display contacts .
 
<apex:page controller="DiplayRelatedContacts" id="pg">
	<apex:form  id="frm">
		<apex:pageBlock id="pgblk" >
			<apex:pageBlockTable value="{!accList}" var="ac">
				<apex:column width="10px">
					<input type="radio" name="group1" />
					<apex:actionSupport event="onclick" action="{!dispalyContact}" ReRender="conpgblk" >
						<apex:param assignTo="{!accId}" name="accname" value="{!ac.id}"/>
					</apex:actionSupport>
				</apex:column>
				<apex:column value="{!ac.Name}" />
				<apex:column value="{!ac.AccountNumber}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="conpgblk" >
			<apex:outputPanel rendered="{!conList.size == 0}">
				<b> NO RELATED CONTACTS FOR THIS ACCOUNT .</b>
			</apex:outputPanel>
			<apex:outputPanel rendered="{!conList.size != 0}">
				<apex:pageBlockTable value="{!conList}" var="con">
					<apex:column value="{!con.FirstName}" />
					<apex:column value="{!con.LastName}" />
				</apex:pageBlockTable>
			</apex:outputPanel>
		</apex:pageBlock>
	</apex:form>
</apex:page>
Apex class related to page .
 
public class DiplayRelatedContacts {
   public List<Account> accList{get;set;}
   public list<Contact> conList{get;set;}
   public String accId{get;set;}
   public DiplayRelatedContacts(){
       accList=[SELECT Id,Name,AccountNumber FROM Account LIMIT 10];
   
   }
    public PageReference dispalyContact() {
       if(accId != null)
       conList=[SELECT id,FirstName,LastName FROM COntact WHERE AccountId=:accId];
        return null;
    }
}
Let me know if it helps !!
Thanks
Manoj

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-

First the Controller
 
public class wrapperClassController {

	//Our collection of the class/wrapper objects cContact 
	public List<cContact> contactList {get; set;}

	//This method uses a simple SOQL query to return a List of Contacts
	public List<cContact> getContacts() {
		if(contactList == null) {
			contactList = new List<cContact>();
			for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
				// As each contact is processed we create a new cContact object and add it to the contactList
				contactList.add(new cContact(c));
			}
		}
		return contactList;
	}


	public PageReference processSelected() {

                //We create a new list of Contacts that we be populated only with Contacts if they are selected
		List<Contact> selectedContacts = new List<Contact>();

		//We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
		for(cContact cCon: getContacts()) {
			if(cCon.selected == true) {
				selectedContacts.add(cCon.con);
			}
		}

		// Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
		System.debug('These are the selected Contacts...');
		for(Contact con: selectedContacts) {
			system.debug(con);
		}
		contactList=null; // we need this line if we performed a write operation  because getContacts gets a fresh list now
		return null;
	}


	// This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
	public class cContact {
		public Contact con {get; set;}
		public Boolean selected {get; set;}

		//This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
		public cContact(Contact c) {
			con = c;
			selected = false;
		}
	}
}

Page
<apex:page controller="wrapperClassController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/>
            </apex:pageBlockButtons>
            <!-- In our table we are displaying the cContact records -->
            <apex:pageBlockTable value="{!contacts}" var="c" id="table">
                <apex:column >
                    <!-- This is our selected Boolean property in our wrapper class -->
                    <apex:inputCheckbox value="{!c.selected}"/>
                </apex:column>
                <!-- This is how we access the contact values within our cContact container/wrapper -->
                <apex:column value="{!c.con.Name}" />
                <apex:column value="{!c.con.Email}" />
                <apex:column value="{!c.con.Phone}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Please mark this as solution if this will help you

Thanks
Amit Chaudhary
ManojjenaManojjena
Hi G04,
You can try with below code which will display related contacts in a page block below .Try to modify the place where you want to display contacts .
 
<apex:page controller="DiplayRelatedContacts" id="pg">
	<apex:form  id="frm">
		<apex:pageBlock id="pgblk" >
			<apex:pageBlockTable value="{!accList}" var="ac">
				<apex:column width="10px">
					<input type="radio" name="group1" />
					<apex:actionSupport event="onclick" action="{!dispalyContact}" ReRender="conpgblk" >
						<apex:param assignTo="{!accId}" name="accname" value="{!ac.id}"/>
					</apex:actionSupport>
				</apex:column>
				<apex:column value="{!ac.Name}" />
				<apex:column value="{!ac.AccountNumber}" />
			</apex:pageBlockTable>
		</apex:pageBlock>
		<apex:pageBlock id="conpgblk" >
			<apex:outputPanel rendered="{!conList.size == 0}">
				<b> NO RELATED CONTACTS FOR THIS ACCOUNT .</b>
			</apex:outputPanel>
			<apex:outputPanel rendered="{!conList.size != 0}">
				<apex:pageBlockTable value="{!conList}" var="con">
					<apex:column value="{!con.FirstName}" />
					<apex:column value="{!con.LastName}" />
				</apex:pageBlockTable>
			</apex:outputPanel>
		</apex:pageBlock>
	</apex:form>
</apex:page>
Apex class related to page .
 
public class DiplayRelatedContacts {
   public List<Account> accList{get;set;}
   public list<Contact> conList{get;set;}
   public String accId{get;set;}
   public DiplayRelatedContacts(){
       accList=[SELECT Id,Name,AccountNumber FROM Account LIMIT 10];
   
   }
    public PageReference dispalyContact() {
       if(accId != null)
       conList=[SELECT id,FirstName,LastName FROM COntact WHERE AccountId=:accId];
        return null;
    }
}
Let me know if it helps !!
Thanks
Manoj

 
This was selected as the best answer
ManojjenaManojjena
Hi G04,
As per your post you want to display near by the account name ,you can try below VF page with above class ,
If you want to display any other field just add the field in contact query and that field in page with one column.
 
<apex:page controller="DiplayRelatedContacts" >
  <apex:form >
   <apex:pageBlock id="pgblk" >
        <apex:pageBlockTable value="{!accList}" var="ac">
            <apex:column width="10px" headerValue="Select">
               <input type="radio" name="group1" />
                <apex:actionSupport event="onclick" action="{!dispalyContact}" ReRender="conpgblk" >
                    <apex:param assignTo="{!accId}" name="accname" value="{!ac.id}"/>
                </apex:actionSupport>
            </apex:column>
            
            <apex:column value="{!ac.Name}" width="30px"/>
            <apex:column >
                    <apex:pageBlock id="conpgblk" mode="maindetail">
                        <apex:outputPanel rendered="{!conList.size == 0}">
                            <b> NO RELATED CONTACTS FOR THIS ACCOUNT .</b>
                        </apex:outputPanel>
                        <apex:outputPanel rendered="{!conList.size != 0}">
                            <apex:pageBlockTable value="{!conList}" var="con">
                                <apex:column value="{!con.FirstName}"  width="15px"/>
                                <apex:column value="{!con.LastName}"  width="15px"/>
                            </apex:pageBlockTable>
                        </apex:outputPanel>
                    </apex:pageBlock>
             </apex:column>
             <apex:column value="{!ac.AccountNumber}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
  </apex:form>
</apex:page>
Let me know if it helps !!
Thanks
Manoj
 
Amit Chaudhary 8Amit Chaudhary 8
Hi G04,

If you want to show account and contact at same time you can also try below code :-
 
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false">
    <apex:pageBlock >
          <apex:repeat value="{!accounts}" var="a">
<apex:pageBlockSection title="{!a.name}"></apex:pageBlockSection>
  <apex:relatedList list="Contacts" subject="{!a.Id}"/>
<apex:relatedList list="Opportunities" subject="{!a.Id}" />
</apex:repeat>      
     </apex:pageBlock>
</apex:page>

Please mark this as solution if this will help you.

Thanks,
Amit Chaudhary
Gayathri Adoni04Gayathri Adoni04
Thank you all for ur solutions..