0

Can anyone help me to sort a field set that I'm iterating through with apex:repeat? I have a Visualforce page that renders some data as a PDF using Field Sets and I just want the ability to sort the Opportunities block by Close Date desc. (I also have a Task set that I aim to sort by Activity Date desc, but I imagine that will have the same answer as this, only posting one block for simplicity.)

My code snippet right now (standardController = "Account"):

<table id="Opportunities">
  <tr>
    <apex:repeat value="{!$ObjectType.Opportunity.FieldSets.Opp_Set}" var="f">
        <th class="tableHeader">{!$ObjectType.Opportunity.fields[f].Label}</th>
    </apex:repeat>
  </tr>
  <apex:repeat value="{!account.Opportunities}" var="opportunity">
    <tr>
      <apex:repeat value="{!$ObjectType.Opportunity.FieldSets.Opp_Set}" var="f">
        <td><apex:outputField value="{!opportunity[f]}"/></td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>

FWIW: I'm self-taught admin who dabbles in development - I feel like I could probably figure this out with more time, but I'm under the wire and feeling the crunch of not being sure how to adapt this.

Looks like I can either go with some Javascript ala Sort Order of Child Records Using a Standard Controller, though I don't really understand how to adapt this to work with Field Sets (Field Sets were totally new to me with this project, so I might be making them out to be more impactful than they really are), or an extension or custom controller, which I just....really didn't want to have to do for this project and am honestly not quite sure how to do either.

Yes, I've already upvoted https://success.salesforce.com/ideaView?id=08730000000Brl4AAC though I encourage others to as well.

Thank you all in advance!

1 Answer 1

2

JavaScript is probably not going to avail you here:

  • Don’t use standard components that aren’t easily formatted for print, or form elements such as inputs or buttons, or any component that requires JavaScript to be formatted.
  • PDF rendering doesn’t support JavaScript-rendered content.

Although I cannot say I've tried it in this context.

You can achieve this with a fairly simple controller extension, although the fact that you're using field sets does complicate the Apex slightly because one must pull the fields and construct a dynamic SOQL query rather than relying upon the Visualforce page's standard controller to handle the dependencies.

Something like the skeleton below should do the trick, but I'm a little leery of steering you down this road without some Apex knowledge. You'll need to try this out, possibly adapt it to your org, wire this up to your page with an extensions= attribute, and you must add a test class to be able to deploy it to production. If you don't have time to work on your development, you may not want to start down this road - or you may need to work with a developer.

(Please also note that I haven't testing this actual code in my dev org. I hope it's a useful starting point).

public with sharing class ContactSortingExtension {
    private ApexPages.StandardController controller;

    public ContactSortingExtension(ApexPages.StandardController c) {
        controller = c;
    }

    public List<Opportunity> getOpportunities() {
        Id accountId;
        List<String> fields = new List<String>();
        String baseQuery = 'SELECT {0} FROM Opportunity WHERE AccountId = \'\'{1}\'\' ORDER BY CloseDate DESC';

        accountId = (Account)controller.getRecord().Id;

        for (Schema.FieldSetMember f : Schema.SObjectType.Opportunity.fieldSets.Opp_Set.getFields()) {
            fields.add(f.getFieldPath());
        }

        return Database.query(
            String.format(baseQuery, 
                          new List<String>{ String.join(fields, ', '), 
                                            accountId }
                         )
        );
    }
}
9
  • Thanks David! I had a hunch with the Javascript + render as PDF issue, but not the link to back it up. I'll tinker with the extension. I've written Apex & VF before and am comfortable with Apex development basics like test classes. I just don't pretend to know best practices (I always put too much logic in triggers; extensions still feel bizarre, etc.) I've made VF pages like this before without FieldSets & really liked FieldSets for the ability to swap out fields without updating the VF or Apex. Won't this be irrelevant if I need to hard code the fields in the SOQL query in the Extension?
    – mlpSFadmin
    Commented Jun 4, 2018 at 18:44
  • Mostly just in a rush because this wasn't in the scope of the project, and I'm pushing it through as a favor for a colleague because printable view doesn't work in Lightning & truncates related lists, and it would have been a hell of a report with a lot of manipulation in Excel for her to get the external facing doc she needs. I should know better than to try new things in VF when the dev work wasn't in the timeline in the first place, but c'est la vie.
    – mlpSFadmin
    Commented Jun 4, 2018 at 18:46
  • 1
    I feel your pain, and best of luck. What we're doing here is reading the Field Set in Apex to build a dynamic query, so you still can administratively configure the displayed fields. We aren't going to hard code the fields, for the reason you articulated.
    – David Reed
    Commented Jun 4, 2018 at 18:52
  • Hi again David. Playing with this, and it feels like it could work. Having a hard time calling the getOpportunities method from my VF. It's just the list in line 7 of my above, swapping out !account.Opportunities with !getOpportunities? Or should I be swapping out the !get...FieldSet lists as well? I thought the former, having issues replacing that method, though I always forget the correct syntax.
    – mlpSFadmin
    Commented Jun 4, 2018 at 20:09
  • It's just {! opportunities} in that context.
    – David Reed
    Commented Jun 4, 2018 at 20:09

You must log in to answer this question.

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