11

How can one get ALL the records from a list view in a visualforce page running in Lightning? (Since no checkbox for selecting are available)

Similar old question.

With an adaptation from that question's answer, I have:

public SomeClass(ApexPages.StandardSetController con) {
    if (con.getSelected().size() > 0)
        lots = [SELECT Name FROM Custom__c WHERE Id IN :con.getSelected()];
    else {
        Transient Set<sObject> sObjects = new Set<sObject>();
        sObjects.addAll(con.getRecords());
        while (con.getHasNext()) {
            con.next();
            sObjects.addAll(con.getRecords());
        }
        lots = [SELECT Name FROM Custom__c WHERE Id IN :sObjects];
    }
}

And this works fine in Salesforce Classic, for each condition. But in lightning it works differently each time, looks like every time I test this, it chooses a different list to get the records. (Looks like a SF bug)

  • Any luck here? Anything you would like for me to change about my answer to be more helpful? – Adrian Larson Feb 29 '16 at 14:59
  • Hi @AdrianLarson I'm just waiting to see if this question gets any official attention before accepting your answer, because basically there is nothing else we can do at this moment to get the list view records in lightning – Jose Feb 29 '16 at 15:36
  • Makes sense. Perhaps someone more clever than I can figure out how to pass the parameter. – Adrian Larson Feb 29 '16 at 15:43
4
+50

Bit of a shot in the dark, but try calling setFilterId on your StandardSetController to get more deterministic behavior.

public static final String DESIRED_LISTVIEW_LABEL = 'All';
public SomeClass(ApexPages.StandardSetController controller)
{
    if (!controller.getSelected().isEmpty())
        return [SELECT Name FROM MyObject__c WHERE Id IN :controller.getSelected()];
    for (SelectOption listViewOption : controller.getListViewOptions())
    {
        if (listViewOption.getLabel() == DESIRED_LISTVIEW_LABEL)
        {
            controller.setFilterId(listViewOption.getValue());
            break;
        }
    }
    // do stuff
}

You can also expose the available filters ( List<SelectOption>) returned from getListViewOptions() as a property on your controller and let the end user decide which list view they want.

Update Lightning does not support Javascript buttons.

You do not need to add an extra user input step. Instead of making DESIRED_LISTVIEW_LABEL final, you can get it through page parameters if you make your list button Execute Javascript as follows:

(function (w) {
    "use strict"
    var fcf, index, payload, search = window.location.search,
        params = search.substring(1, search.length).split('&');
    for (i in params) {
        payload = params[i].split('=');
        if (payload[0] == 'fcf') {
            fcf = payload[1];
            break;
        }
    }
    w.open('/apex/MyPage?ListView=' + fcf);
})(window);

You could simplify the above somewhat since fcf seems to be the only parameter most of the time, but I tried to make it robust against future changes in the url. Then you can do:

public static final String LISTVIEW_PARAM = 'ListView';
public Id desiredFilterId
{
    get
    {
        return ApexPages.currentPage().getParameters().get(LISTVIEW_PARAM);
    }
}
public SomeClass(ApexPages.StandardSetController controller)
{
    if (!controller.getSelected().isEmpty())
        return [SELECT Name FROM MyObject__c WHERE Id IN :controller.getSelected()];
    if (String.isNotBlank(desiredFilterId))
        controller.setFilterId(desiredFilterId);
    // do stuff
}
  • Hi Adrian, I was trying to test your workaround but couldn't from the apex class (no problem from execute anonymous), I ran into sObject type 'ListView' is not supported. problem. I read your response at salesforce.stackexchange.com/questions/64004/… but I didn't understand the solution – Jose Feb 26 '16 at 13:51
  • @Jose For some reason I thought SOQL would work. Anyway, I updated it with a better code sample that should work for you. – Adrian Larson Feb 26 '16 at 14:26
  • Thanks Adrian, this works perfectly as a workaround (with an extra step for the end user), but the question/bug remains in place, why the StandardSetController isn't getting the list ID in context? – Jose Feb 26 '16 at 15:54
  • @Jose I think the issue is just that there is no context Id. The defaulting behavior in classic seems to match what you expect, whereas the defaulting behavior in Lightning seems more arbitrary. It shouldn't be necessary to add a step for the end user, though. At least not as I understand it. – Adrian Larson Feb 26 '16 at 16:07
  • 1
    Let us continue this discussion in chat. – Jose Feb 26 '16 at 16:40

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.