How can I query and access a specific field on Contacts to then help me populate a Visualforce page I have on the Account?


  • I have a Visualforce page on my Contact that simply displays an alert if our field "Special_Care_Comments__c" is not blank.

  • I'd like to roll this up to the Account level too. So, if any Contact on the Account has their "Special_Care_Comments__c" field filled in, I want to display the same type of simple alert popup on the Account.

This is my Contact VF page:

 <apex:page standardController="Contact">
    <script type="text/javascript">
        var specialCareMessage = "{!Contact.Special_Care_Comments__c}";

        if( specialCareMessage !== "" ) {
            alert( "SPECIAL CARE COMMENTS: "+ specialCareMessage);

How can I query all Contacts belonging to the Account, and, if possible, concatenate the values in this special care comments field to display in the alert popup?

  • You're going to need a VIsualforce controller extension that performs a SOQL parent-child query using the relationship name Contacts. Your controller extension can perform the concatenation and return data to the page.
    – David Reed
    Feb 23, 2018 at 21:18
  • Okay, good, that's what I thought. Would you be able to provide me an example though?
    – Naomi
    Feb 23, 2018 at 21:19
  • I do not have an example to hand, but this is a pretty simple build that I think you can get through with the docs and Trailhead. You can ask further questions when you get stuck.
    – David Reed
    Feb 23, 2018 at 21:48
  • @DavidReed No Apex Code necessary for this one.
    – sfdcfox
    Feb 23, 2018 at 21:57

1 Answer 1


For the account page, you can use some standard controller logic, like this:

<apex:page standardController="Account">
      var messages = [], item;
      <apex:repeat value="{!Account.Contacts}" var="Contact">
        item = "{!JSENCODE(Contact.Special_Care_Comments__c)}";
        if(item) {
      if(messages.length) {

If you want deduplication, etc, you'll have to figure that out on your own.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .