In an VisualForce page I am using a standardController on the Quote object. I also have an extension that has a constructor that takes in a standardSetController. The standardSetController.getRecords() returns unrelated records.

VisualForce Page:

<apex:page id="SmallBusinessRenewalQuote" standardController="Quote" 
recordSetVar="quotes" extensions="SmallBusinessRenewalQuoteController">


public List<Quote> quoteList { get; set; }
public ApexPages.StandardSetController controller { get; set; }

public SmallBusinessRenewalQuoteController(ApexPages.StandardSetController standardSetController){
    controller = standardSetController;
    quoteList = (List<Quote>)controller.getRecords();
    System.debug('Quote List: + quoteList);
    System.debug('Quote List Size: + quoteList.size());

There is a button on a related list of quotes that we use to access the Visual Force page. There is only 1 quote on this specific related list, and the getRecords() method is returning 22.

  • Not 100% what you are trying to do. If you have a button on the related object and you want to display the associated quotes, you need to specify the Id of related object to filter the only the quotes associated with it. – Andrew L. Jun 1 '16 at 20:39

I can see why this functionality does not work in a related list. It's frustrating, but there is no list view to filter by, and that's really what the StandardSetController is using to figure out the results for getRecords().

All hope is not lost, however. You can use the page headers to figure out what the previous url is. This information can be parsed to get the parent Id. The following constructor will work whether used on a related list button or list view button:

public with sharing class MyObjectListExtension
    public List<MyObject__c> records { get; private set; }
    public MyObjectListExtension(ApexPages.StandardSetController controller)
        String referer = ApexPages.currentPage().getHeaders().get('Referer');
            String substring = referer.contains('?') ?
                referer.substringBetween('.com/', '?') : referer.substringAfter('.com/');
            Id parentId = Id.valueOf(substring);
            records = [SELECT Name FROM MyObject__c WHERE Parent__c = :parentId];
        catch (StringException se)
            records = controller.getRecords();

You can add the following to your constructor to get a better idea of what's going on under the hood. When you click the related list button, you should see the All list view with an Id of 000000000000000AAA.

public MyObjectListExtension(ApexPages.StandardSetController controller)
    for (SelectOption option : controller.getListViewOptions())
        if (option.getValue() == controller.getFilterId())
| improve this answer | |

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.