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
Megan Moody 10Megan Moody 10 

Get value from current record in Apex class

I'm a novice at Apex and VF development. I'm just trying to create a VF page that displays all contact records where a particular field equals a value from the current record, which is a contact record. 

My VF code is using this apex controller code. I am getting a blank back in the Subscriber_ID__c value. 

I'm surely missing a simple step in my code. Can anyone help?

public with sharing class ConciergeDisplayFamilyMembers

    public String subscriberID {get;set;}
    public List<Contact> Records {get; set;} 
    
    public ConciergeDisplayFamilyMembers()
    { 
        Records = 
            [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :subscriberID]; 
    } 

public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc)    
    {            
    subscriberID = ApexPages.CurrentPage().getparameters().get('Subscriber_ID__c');
    }
}
 
Best Answer chosen by Megan Moody 10
Megan Moody 10Megan Moody 10
I contacted Salesforce support to get help with this problem. Here's the answer in case anyone references this post in the future: 
 
public class ConciergeDisplayFamilyMembers
{ 
    public Contact currentContact {get;set;}     
    
    public List<Contact> Records
    {
        
        get
        {
            try
            {
                Records = new List<Contact>();
                Records = [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :currentContact.Subscriber_ID__c and RecordTypeId = '012q00000004SAiAAM']; 
            } 
            catch (Exception ex)
            {
                  Records = null;
            }
            return Records;
        }
        private set;
    }
        

    public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc)
    {
        sc.AddFields(new List<String>{'Subscriber_ID__c'});
        currentContact = (Contact) sc.getRecord();     
        System.debug('***** ' + currentContact );
    }

}

 

All Answers

BalajiRanganathanBalajiRanganathan
How are you using this class in your VF? is this class is a controller or controller extn?
I see that you are using two constructor. but only one will be called.

If your VF is using your class as controller extension then put all you code on ConciergeDisplayFamilyMembers(ApexPages.StandardController sc) .else put all your code on  ConciergeDisplayFamilyMembers()
James LoghryJames Loghry
By the way your describing you're issue, it sounds like you are using an Apex extension controller in conjunction with a standard controller representing a Subscribr?  If so, could you response with the first tag in your Visualforce page?

It looks like you still have an issue here in your extension's constructor.  By default, standard controllers will supply extensions with the standard Id field, from which you can use in your query.  For an example, see the modified extension constructor below:
 
public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc){            
    subscriberID = sc.getRecordId();
    records = 
        [Select 
            Name
            ,Gender__c
            ,Subscriber_Id__c
            ,Member_Id_Suffix__c
            ,Member_Id__c 
         From 
            Contact 
         Where 
            Subscriber_Id__c = :subscriberId];
}


For more info on extension controllers, please see this document: http://www.salesforce.com/docs/developer/pages/Content/pages_controller_extension.htm
Jagan ReddyJagan Reddy
Hi,

try to create instance first then you will get expected output.

public with sharing class ConciergeDisplayFamilyMembers

    public String subscriberID {get;set;}
    public List<Contact> Records {get; set;} 
    
    public ConciergeDisplayFamilyMembers()
    { 
        Records = new List<Contact>();
        Records = 
            [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :subscriberID]; 
    } 

public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc)    
    {            
    subscriberID = ApexPages.CurrentPage().getparameters().get('Subscriber_ID__c');
    }
}
Megan Moody 10Megan Moody 10
So far no luck. Here's my current code:

VF page
<apex:page standardcontroller="Contact" extensions="ConciergeDisplayFamilyMembers"> 
    <apex:pageBlock > 
        <apex:pageBlockTable value="{!Records}" var="Record"> 
            <apex:column > 
                <apex:facet name="header">Name</apex:facet> 
                <apex:outputText value="{!Record.Name}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Member ID</apex:facet> 
                <apex:outputText value="{!Record.Member_ID__c}"/> 
            </apex:column>
            <apex:column > 
                <apex:facet name="header">suffix</apex:facet> 
                <apex:outputText value="{!Record.Member_ID_suffix__c}"/> 
            </apex:column>  
            <apex:column > 
                <apex:facet name="header">Gender</apex:facet> 
                <apex:outputText value="{!Record.Gender__c}"/> 
            </apex:column> 
            <apex:column > 
                <apex:facet name="header">Subscriber ID</apex:facet> 
                <apex:outputText value="{!Record.Subscriber_ID__c}"/> 
            </apex:column> 
        </apex:pageBlockTable> 
    </apex:pageBlock> 
</apex:page>

Apex class
 
public with sharing class ConciergeDisplayFamilyMembers
{ 
    public String subscriberID {get;set;}
    public List<Contact> Records {get; set;} 
    
    public ConciergeDisplayFamilyMembers()
    { 
        Records = new List<Contact>();
        Records = 
            [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :subscriberID RecordTypeId = '012q00000004SAiAAM']; 
    } 

public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc)    
    {            
    subscriberID = ApexPages.CurrentPage().getparameters().get('Subscriber_ID__c');
    }
}

I don't know if this clarifies/helps, but what I want to display are all contacts who are family members of the currently displayed contact. This is determined based on the SubscriberID which will be the same for all family members. All information is stored in the Contact object. Subscriber ID is obviously a custom field.

I'm not getting any errors - just a blank list. Any more thoughts?  
Megan Moody 10Megan Moody 10
Also, if I change my VF page to remove the standardController of Contact and make ConciergeDisplayFamilyMembers the controller, the page will display all the contacts with a blank subscriber ID of the designated record type. 

That is, if I leave the Apex class as it is shown in the post above, but make this one change to the VF page code, the results are different. 

First code line: <apex:page controller="ConciergeDisplayFamilyMembers"> 
Displays all contacts where subscriber ID is null and of record type '012q00000004SAiAAM'
 
First code line: <apex:page standardcontroller="Contact" extensions="ConciergeDisplayFamilyMembers">
Displays an empty list, but no error on the VF page. 

So the question is why can I not get the value from the custom field Subscriber_ID__c into the variable run in the SOQL query? 
Megan Moody 10Megan Moody 10
I contacted Salesforce support to get help with this problem. Here's the answer in case anyone references this post in the future: 
 
public class ConciergeDisplayFamilyMembers
{ 
    public Contact currentContact {get;set;}     
    
    public List<Contact> Records
    {
        
        get
        {
            try
            {
                Records = new List<Contact>();
                Records = [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :currentContact.Subscriber_ID__c and RecordTypeId = '012q00000004SAiAAM']; 
            } 
            catch (Exception ex)
            {
                  Records = null;
            }
            return Records;
        }
        private set;
    }
        

    public ConciergeDisplayFamilyMembers(ApexPages.StandardController sc)
    {
        sc.AddFields(new List<String>{'Subscriber_ID__c'});
        currentContact = (Contact) sc.getRecord();     
        System.debug('***** ' + currentContact );
    }

}

 
This was selected as the best answer