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;
}
}
}
Attribution to: ModestCowboyDan
Possible Suggestion/Solution #1
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.
Attribution to: James Loghry
Possible Suggestion/Solution #2
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!
Attribution to: JKnight
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/709