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 current contact in Apex class

I have a VF page in a Service Cloud console. I want it to display all other contacts with the same value in a custom field of the current contact. For some reason my Apex class won't get the current contact record. My two code snippets are below. Anyone with ideas as to why this isn't working? Any help is much appreciated. 
 
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)
    {
        currentContact = (Contact) sc.getRecord();
    }

}
 
<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>

In the apex class, if I leave out the part of the WHERE clause that is searching for the custom field value (
WHERE Subscriber_ID__c = :currentContact.Subscriber_ID__c) and just search on the record type, it returns records. But with the Subscriber_ID__c field, it never returns any records. 
Best Answer chosen by Megan Moody 10
Megan Moody 10Megan Moody 10
I contacted Salesforce support to get help resolving this issue. Posting the answer in case others reference 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

Mathew Andresen 5Mathew Andresen 5
I've always gotten the Id of a record by using something like 
Id contactId = ApexPages.currentPage().getParameters().get('id');

So then you would change your query to
 
Records = [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE Subscriber_ID__c = :contactId AND RecordTypeId = '012q00000004SAiAAM'];

I'm not an expert but I expect your currentContact get; set;   isn't returning anything.  you could set this with system.debug, and I expect you don't have a value to assign.

I hope this helps,
Megan Moody 10Megan Moody 10
It actually is returning the ID. If I use the current code, but then change the SOQL to ...

Records = [SELECT Name,Gender__c,Subscriber_ID__c,Member_ID_suffix__c,Member_ID__c FROM Contact WHERE ID = :currentContact.ID];

...it does return the current record. So the ID of the current record is being returned. However, what's not being returned are the values in the other fields. Everything I've researched says I should be able to do :currentContact.fieldname to use reference the value in at field of the current record. 
Megan Moody 10Megan Moody 10
I contacted Salesforce support to get help resolving this issue. Posting the answer in case others reference 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