You need to sign in to do that
Don't have an account?
Megan 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.
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.
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.
All Answers
So then you would change your query to
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,
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.