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
SFDC GuestSFDC Guest 

Display records in PageBlockTable with Radio buttons

Hi All,

I have created custom fields Credit_Card_Name__c, Credit_Card_Number__c in Contact object. And i have created one custom object - Credit_Card__c with fields Name, Card_Holder_Email__c, Credit_Card_Number__c. 
I have created one custom button (name: Credit Card) on contact object of type (Display Type: Detail Page Button, Content Source: Visualforce Page).
I have inserted the records for Credit Card object.

When I click on custom button (name: Credit Card), I want to retrieve and display only those records which are related to Contact email in the form PageBlockTable with Radio buttons. After selecting one credit card, those details should be displayed in Contact object fields Credit_Card_Name__c, Credit_Card_Number__c. Please help to achieve this.

Below is the controller & vf page i tried.

public Class ContactCreditDetailsPage
{


    public Contact con {get; set;}
    public List<Credit_Card__c> ccard {get; set;}
    
    public ContactCreditDetailsPage(ApexPages.StandardController stdCtr)
    {
        con = (Contact)stdCtr.getRecord();
        con = [select id, email from contact limit 1];
       
    }
    
    public List<Credit_Card__c> getRecords()
    {
         ccard = new List<Credit_Card__c> ();
        List<Credit_Card__c> results= [select Card_Holder_Email__c, Name, Credit_Card_Number__c from Credit_Card__c where  Card_Holder_Email__c=: con.Email]; //where Card_Holder_Email__c=: con.Email
        return results;
    }
    

}

VF page:
<apex:page standardController="Contact" extensions="ContactCreditDetailsPage" >
  <apex:form >
      <apex:pageBlock >
         <apex:pageBlockTable value="{!Records}" var="cc">
             <apex:column value="{!cc.Card_Holder_Email__c}"/>
              <apex:column value="{!cc.Credit_Card_Number__c }"/>
              <apex:column value="{!cc.Name}"/>
              
         </apex:pageBlockTable>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Thanks
 
Best Answer chosen by SFDC Guest
Satish PrajapatSatish Prajapat
Hello Sohel,
I understood and I solved your problem.
see the code below:
//Apex class
public Class ContactCreditDetailsPage
{
    private final Contact conObject = null;
    public String selectedCard{get; set;}
    public List<Contact> con2 {get; set;}
    public List<Credit_Card__c> ccard {get; set;}
    public List<Credit_Card__c> results;
    public ContactCreditDetailsPage(ApexPages.StandardController stdCtr)
    {
        ccard = new List<Credit_Card__c> ();
        this.conObject = (Contact)stdCtr.getRecord();
        con2 = [select id, email, name from contact where id=:conObject.Id];
    }
    public List<SelectOption> getRecords()
    {
       	results = [select Name, Card_Holder_Email__c, Credit_Card_Number__c from Credit_Card__c where  Card_Holder_Email__c =: con2.get(0).Email]; 
        List<SelectOption> cardList = new List<SelectOption>();
        for(Credit_Card__c cc : results)
        {
            cardList.add(new SelectOption(cc.Name,cc.name));
        }
        return cardList;
    }
    public void m1()
    {
        for(Credit_Card__c cc : results)
        {
            if(selectedCard == cc.Name)
            {
                contact conObj = new contact();
                conObj.Credit_Card_Number__c = cc.Credit_Card_Number__c;
                conObj.Credit_Card_Name__c = cc.Name;
                conObj.Id = con2.get(0).Id;
                System.debug('conObj'+conObj);
                update conObj;    
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////
//VF Page
<apex:page standardController="Contact" extensions="ContactCreditDetailsPage" >
    <apex:form >
        <apex:pageBlock >          
            <apex:selectRadio value="{!selectedCard}" >
                <apex:selectOptions value="{!Records}"/>
            </apex:selectRadio>
            <apex:commandButton action="{!m1}" title="Add selected card info in Contact" value="Add Card" />
        </apex:pageBlock>
    </apex:form>
</apex:page>
Please Mark as best Answer if this is helpful for you else tell me your problem.

All Answers

AvaneeshAvaneesh
Hi Sohel 

i read your problem bt i didn't understand your flow ....
but let me tell you if you want a checkbox with every record after Query then you have to work on Wrapper Classes
create your own wrapper class with that object and checkbox .
if you explain your problem then i can write your wrapper class
 
Public class your Extension/controller_Name
{
// declare your variables 

//Wrapper class for your contact 

public class WrapperClassForContact
{
public Contact con {get;set;}
public boolean checkbox{get;set;}
// declare your constructor of wrapper class
public WrapperClassForContact(Contact cc)
{
this.con=cc;
}

}

}

this is way how to declare wrapper class ....................read this link 
https://developer.salesforce.com/page/Wrapper_Class
https://success.salesforce.com/answers?id=90630000000hSoLAAU
http://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/



Please Mark as best Answer if this is helpful  

thank you 
Avaneesh Singh

 
Satish PrajapatSatish Prajapat
Hello Sohel,
I understood and I solved your problem.
see the code below:
//Apex class
public Class ContactCreditDetailsPage
{
    private final Contact conObject = null;
    public String selectedCard{get; set;}
    public List<Contact> con2 {get; set;}
    public List<Credit_Card__c> ccard {get; set;}
    public List<Credit_Card__c> results;
    public ContactCreditDetailsPage(ApexPages.StandardController stdCtr)
    {
        ccard = new List<Credit_Card__c> ();
        this.conObject = (Contact)stdCtr.getRecord();
        con2 = [select id, email, name from contact where id=:conObject.Id];
    }
    public List<SelectOption> getRecords()
    {
       	results = [select Name, Card_Holder_Email__c, Credit_Card_Number__c from Credit_Card__c where  Card_Holder_Email__c =: con2.get(0).Email]; 
        List<SelectOption> cardList = new List<SelectOption>();
        for(Credit_Card__c cc : results)
        {
            cardList.add(new SelectOption(cc.Name,cc.name));
        }
        return cardList;
    }
    public void m1()
    {
        for(Credit_Card__c cc : results)
        {
            if(selectedCard == cc.Name)
            {
                contact conObj = new contact();
                conObj.Credit_Card_Number__c = cc.Credit_Card_Number__c;
                conObj.Credit_Card_Name__c = cc.Name;
                conObj.Id = con2.get(0).Id;
                System.debug('conObj'+conObj);
                update conObj;    
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////
//VF Page
<apex:page standardController="Contact" extensions="ContactCreditDetailsPage" >
    <apex:form >
        <apex:pageBlock >          
            <apex:selectRadio value="{!selectedCard}" >
                <apex:selectOptions value="{!Records}"/>
            </apex:selectRadio>
            <apex:commandButton action="{!m1}" title="Add selected card info in Contact" value="Add Card" />
        </apex:pageBlock>
    </apex:form>
</apex:page>
Please Mark as best Answer if this is helpful for you else tell me your problem.
This was selected as the best answer
SFDC GuestSFDC Guest
Hi Satish,

I am getting below error after clicking on button "Add Card". After clicking on Add Card button, page should redirect to Contact page.

Visualforce Error

System.StringException: Invalid id: Gold Card
Error is in expression '{!m1}' in component <apex:commandButton> in page contactcreditdetailspage: Class.ContactCreditDetailsPage.m1: line 32, column 1
Class.ContactCreditDetailsPage.m1: line 32, column 1

 
SFDC GuestSFDC Guest
And also, page should display the all Credit cards details in the form of page block table along with Radio buttons.

as below

Radiobutton1       CreditCardName2   CreditCardNumber2
Radiobutton2       CreditCardName2  CreditCardName2
Satish PrajapatSatish Prajapat
In my org the functionality is working correctly.
As I understood Sohel:
  • you want to display all the card name from Credit_Card__c Object whose email is same as the contact on which Add Card  button placed.
  • After displaying the card name from Credit_Card__c Object, select any one card detail and fill these detainls in Contact object.
Sohel is it your problem ??
If not then explain your problem correctly and what are the records you saved in your custom object.
 
SFDC GuestSFDC Guest
Thanks Satish. It's working fine. But page is not redirecting to Contact page after clicking on  "Add Card".
Could you please make it redirect to Contact details page .

 
Satish PrajapatSatish Prajapat
Yes Sohel,
we can redirect very easily, check code below:
//Apex Class
//Test2VF
public Class ContactCreditDetailsPage
{
    private final Contact conObject = null;
    public String selectedCard{get; set;}
    public List<Contact> con2 {get; set;}
    public List<Credit_Card__c> ccard {get; set;}
    public List<Credit_Card__c> results;
    public ContactCreditDetailsPage(ApexPages.StandardController stdCtr)
    {
        ccard = new List<Credit_Card__c> ();
        this.conObject = (Contact)stdCtr.getRecord();
        con2 = [select id, email, name from contact where id=:conObject.Id];
    }
    public List<SelectOption> getRecords()
    {
       	results = [select Name, Card_Holder_Email__c, Credit_Card_Number__c from Credit_Card__c where  Card_Holder_Email__c =: con2.get(0).Email]; 
        List<SelectOption> cardList = new List<SelectOption>();
        for(Credit_Card__c cc : results)
        {
            cardList.add(new SelectOption(cc.Name,cc.name));
        }
        return cardList;
    }
    public void m1()
    {
        for(Credit_Card__c cc : results)
        {
            if(selectedCard == cc.Name)
            {
                contact conObj = new contact();
                conObj.Credit_Card_Number__c = cc.Credit_Card_Number__c;
                conObj.Credit_Card_Name__c = cc.Name;
                conObj.Id = con2.get(0).Id;
                System.debug('conObj'+conObj);
                update conObj;    
            }
        }
        
    }
    public PageReference save() 
    {
    	PageReference nextPage = new PageReference('/' + con2.get(0).Id);
        m1();
        return nextpage;
    }
    
}

//VF Page
//Replace m1 by save
<apex:page standardController="Contact" extensions="ContactCreditDetailsPage" >
    <apex:form >
        <apex:pageBlock >          
            <apex:selectRadio value="{!selectedCard}" >
                <apex:selectOptions value="{!Records}"/>
            </apex:selectRadio>
            <apex:commandButton action="{!save}" title="Add selected card info in Contact" value="Add Card" />
        </apex:pageBlock>
    </apex:form>
</apex:page>

Please select as a best answer.
If your problem is solved.
Thanks Sohel.
SFDC GuestSFDC Guest
Hi Satish,

Can we show how to display in pageBlockTable along with radio buttons using wrapper class.