6

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

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.

  • You were able to understand, as that did indeed do the trick. I modified it slightly, to bypass my formula field. this.personParentId = [Select Person__r.AccountID From Advancement_Contact_Record__c Where Id = :this.acrId].Person__r.AccountID; – ModestCowboyDan Aug 21 '12 at 16:03
  • Yeah I misread the code originally. Removed the edit. – James Loghry Aug 21 '12 at 17:23
  • Thank you for your help, I am obviously still learning Apex... The net result of this is going to be great for our end users. Our team will be able to see the photos of the Contacts they are communicating with in the context of the work the Advancement team is doing. – ModestCowboyDan Aug 21 '12 at 17:25
3

You can also do the following if you include the Person__r.AccountId field somewhere in your VF page (it can be hidden):

public with sharing class ShowACRPersonPicture{
    public  Attachment  personFile { set; get; }
    public  Boolean     hasPersonPicture { set; get; }

    private Advancement_Contact_Record__c  acr { set; get; }

    public ShowACRPersonPicture(ApexPages.StandardController stdACRController ){
        this.acr = (Advancement_Contact_Record__c)stdACRController.getRecord();        
        this.hasPersonPicture = false;

        if(acr.Person__r.AccountId != null){
            Attachment attPersonList = [Select ParentId, Name, Id, ContentType, BodyLength 
                                     From Attachment
                                     where ParentId =:acr.Person__r.AccountId and Name ='Contact Picture' limit 1][0];

            if(attPersonList != null){
                this.personFile       = attPersonList;
                this.personFile.Body  = Blob.valueOf('AuxString');
                this.hasPersonPicture = true;
            }
        }
    }
}

It saves you a query!

  • That is helpful, thanks! I am already checking to make sure it is an Advancement_Contact_Record__c (via this: if(this.recordId.startsWith('a02')){ ) because there is more to this Class, and it is called from other records that don't need to query off a lookup. – ModestCowboyDan Aug 21 '12 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.