[SalesForce] Retrieving custom fields from a standard controller in an extension

How do I retrieve custom fields from a standard controller in a Visualforce extension controller?

In the code below, if I remove the hard-coded ID "this.personParentId = '001Z000000CkZ4m';", it fails to work.

I assume this is because I don't understand how to move properly dynamically select that field. The net result of this code should be the selection of an Attachment on the Account record that is related to my custom object (Advancement_Contact_Record__c).

The field "Person_Account_ID_c" is a formula (it's just Person_r.AccountID) that does properly display the ID I need.

/**
* @author Sebastian Munoz
* @createdDate 04/06/2010
* Copied and Modified by Jonathan Maher 08.22.12
*/

public with sharing class ShowACRPersonPicture{
    public  Attachment  personFile { set; get; }
    public  Boolean     hasPersonPicture { set; get; }
    private String      personParentId { set; get; }
    private String      acrID { set; get; }

    /**
    * Constructor
    */

    public ShowACRPersonPicture( ApexPages.StandardController stdACRController ){
        this.acrId = stdACRController.getId();        
        this.personParentId =  string.valueof([SELECT Person_Account_ID__c from Advancement_Contact_Record__c WHERE ID = 'a02Z00000016O7P' limit 1]);  
        this.personParentId = '001Z000000CkZ4m';
        this.hasPersonPicture = false;

        List<Attachment> attPersonList = [ Select ParentId, Name, Id, ContentType, BodyLength 
                                         From Attachment
                                         where ParentId =: this.personParentId  and name = 'Contact Picture' limit 1];
        if( attPersonList.size() > 0 ){
            this.personFile       = attPersonList.get( 0 );
            this.personFile.Body  = Blob.valueOf('AuxString');
            this.hasPersonPicture = true;
        }
    }
}

Best Answer

I'm having a hard time understanding your question, but from what I gather: Changing this.personParentId = string.valueof([SELECT Person_Account_ID__c from Advancement_Contact_Record__c WHERE ID = 'a02Z00000016O7P' limit 1]);

to this.personParentId = [Select Person_Acount_Id__c From Advancement_Contact_Record__c Where Id = :this.acrId].Person_Account_Id__c should do the trick.

This is assuming that Person__r.AccountId returns a value of course.

Also, make sure that the profile has read permissions on the Person__r field in Advancement_Contact_Record_c as well as the Person_c object and the AccountId field on the Person__c object.

Related Topic