0

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

Details:

  • 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);
        }
    </script>
</apex:page>

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?

4
  • 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 '18 at 21:18
  • Okay, good, that's what I thought. Would you be able to provide me an example though? – Naomi Feb 23 '18 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 '18 at 21:48
  • @DavidReed No Apex Code necessary for this one. – sfdcfox Feb 23 '18 at 21:57
1

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

<apex:page standardController="Account">
    <script>
      var messages = [], item;
      <apex:repeat value="{!Account.Contacts}" var="Contact">
        item = "{!JSENCODE(Contact.Special_Care_Comments__c)}";
        if(item) {
          messages.push(item);
        }
      </apex:repeat>
      if(messages.length) {
        alert(messages.join('\n'));
      }
    </script>
</apex:page>

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

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.