0

I'm trying to build a Visualforce page that will display Contact Roles for the parent Account of a custom object. My class and page tags follow. I'm not receiving any errors at this time, and the Visualforce page displays, but the Contact Roles are not being listed.

Any thoughts as to where I've made an error here?

Class:

public class AccountContactRoleExtension{

        /*
        A List Type Collection that will hold
        all the Account Contact Roles
    */
    public List ContactRoles{
        get;
        set;

    }

    public PI_Reference__c PI;

    public AccountContactRoleExtension(
        ApexPages.StandardController stdController
    ){  

        this.PI = (PI_Reference__c)stdController.getRecord();

        /*
            Query the Database to retrieve all
            the Contact Roles of the Account
            in Context

        */

        ContactRoles = 
            [
                SELECT 
                    Contact.Name,
                    Contact.Email,
                    Role
                FROM   
                    AccountContactRole
                WHERE  
                    AccountId = 
                        :PI.PI_Reference_Account__r.ID
            ];

    }
}

Page:

<apex:pageBlock title="Account Contact Roles" mode="edit">
    <apex:pageBlockTable value="{!ContactRoles}" var="ContactRole">
        <apex:column headerValue="Name" value="{!ContactRole.Contact.Name}"/>
        <apex:column headerValue="Email" value="{!ContactRole.Contact.Email}"/>
        <apex:column headerValue="Role" value="{!ContactRole.Role}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

Thank you for your help!!

  • Have you run that query and verified that it returns results for the AccountId in question? – Adrian Larson May 4 '15 at 18:53
  • can you add you complete page and in the controller at the following line syntax is not correct public List ContactRoles{get;set;}. This line of code is wrong. can you checkit. It should be public List <ContactRoles> contactRoles{get;set;} – Suresh May 4 '15 at 19:02
1

If your use case is when editing an existing PI_reference__c you want to show the corresponding AccountContactRoles, then

public class AccountContactRoleExtension{

  public List<AccountContactRole> ContactRoles{
    get {return [select contact.name,contact.email,role
              from AccountContactRole
              where accountId = : this.pi.pi_reference_account__c];}
    private set;

  }

  public PI_Reference__c PI;

  public AccountContactRoleExtension(ApexPages.StandardController stdController){
     // following line required only if field(s) not present on VF page  
     stdController.addFields(new List<String>{'pi_reference_account__c'});
     this.PI = (PI_Reference__c)stdController.getRecord();
  }
}

When you call getRecord(), only the actual sobject fields defined on the VF page are fetched; any other fields that you need to have in memory have to be either added to the VF page as hidden fields, added to the VF page as apex:variables, added to the addFields(..) call before doing getRecord(), or queried for in the controller.

Note I also moved the soql into the getter so if another browser tab or user updates the contract roles, you'll always see the latest values.

If your use case includes showing the contactroles on creating a new PI_Reference__c, you may need to pass the accountId as a querystring param and use accordingly

A terrific resource to read on practical VF coding is here.

| improve this answer | |
  • Thank you for your response; this is incredibly helpful. The VF page works now. Thank you as well for the recommended reading. I am definitely going to take a look - I know I have a lot of learning to do where Apex and VF are concerned. – Greg May 5 '15 at 13:02
0

In your controller i can see line which is not correct.

ContactRoles = 
            [
                SELECT 
                    Contact.Name,
                    Contact.Email,
                    Role
                FROM   
                    AccountContactRole
                WHERE  
                    AccountId = 
                        :PIAccountID
            ];

In where clause you should pass AccountId but your code is passing Page Reference Id.

I would suggest to go through this article to understand how Controller extension works and how parameters can be passed from page.

| improve this answer | |
  • Thank you for your help! I read the references material and made some edits to the code above. However, I'm getting a new error now - "SObject row was retrieved via SOQL without querying the requested field: PI_Reference__c.PI_Reference_Account__r". I saw that I may need to use addfields but could not get it to work right. Any thoughts as to what my error is here? – Greg May 4 '15 at 22:29
  • Hi Greg, have you updated code in your question and on which line you are getting error ? – Jitendra Zaa May 5 '15 at 3:15

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.