0

So i have written a controller extension, that fetches Opportunities. Everything worked as it should, life is great. But it displayed only 20 records... my way of fixxing that was adding the line

controller.setPageSize(controller.getResultSize());

but after that my test class was no longer working, I either got the below error:

System.NullPointerException

Or:

External Entry Point error

Needless to say I am quite new and flustered/lost. Any help would be appreciated very much.

Controller code

public with sharing class AKController {
 ApexPages.StandardSetController controller;  

public AKController(ApexPages.StandardSetController controller) 
{
this.controller = controller;
    controller.setPageSize(controller.getResultSize());
}  
    public ApexPages.StandardSetController opportunityRecords 
    {
    get 
    {
        if(opportunityRecords == null) {
            opportunityRecords = new ApexPages.StandardSetController(
                Database.getQueryLocator([stuff_i_query_for]));
        }
        return opportunityRecords;
    }
    set;
    }
public List<Account> getopportunityRecords() {
     return (List<Account>) opportunityRecords.getRecords();
}  

I have tried to put the setpagesize line right after the query but that did not work properly. The Error message said in its Stack Trace that the problems are within this line though and in the 2nd to last line of my Testcode.

Testclass

@isTest class AKCTest 
{
public static testmethod void akcTest() {
PageReference pageRef = Page.aktiveKunden;
Test.setCurrentPage(pageRef);
// Create test data
Account a = new Account(Name='Test');
insert a;
Opportunity o = new Opportunity(AccountId=a.Id, Name='Test', 
CloseDate=Date.today(), StageName='Closed Won', Amount=0.00);
insert o;

        Test.startTest();

AKController Controller = new AKController(null);  

System.assertEquals(1, Controller.getOpportunityRecords().size());
}    
}

while I am aware that there is a null in the parenthesis, it did work in the beginning and still does if the setpagesize command is not there.

  • A Set controller usually displays records in pages. Why would you set the page size to the resultset size? What are you trying to do in that page? – Sebastian Kessel Aug 17 '17 at 15:38
2

You need to pass in a StandardSetController, because "controller" will be null otherwise. At minimum, you can do this:

AKController Controller = new AKController(
  new StandardSetController(
    new List<Opportunity> { o }));  

Note that the reason why it was not showing "all" opportunities at once is because StandardSetController is meant to be a pagination device. Also note that you're constructing a new StandardSetController in your controller itself as well.

If you don't plan on using the out-of-the-box query, you don't need do anything with the parameter. Here's how I would recommend doing your controller:

public with sharing class AKController {
    public ApexPages.StandardSetController opportunityRecords { get; set; }

    public AKController(ApexPages.StandardSetController controller) {
        opportunityRecords = new ApexPages.StandardSetController(
                Database.getQueryLocator([stuff_i_query_for])
        );
    }
    public List<Account> getopportunityRecords() {
        return (List<Account>) opportunityRecords.getRecords();
    }
}
  • thank you, i am aware that i am constructing 2 controllers there, but that has been the only way i found to actually get the page to (consistantly) show more then 20 records... if i´d use a simpler controller like yours, can i solve this issue on the vf page? the controller you recommended works, but only displays 20 records like all my other futile attempts. – Luke Aug 17 '17 at 16:48
  • @Luke You can set the page size on the standard set controller in your controller's constructor, as in opportunityRecords = new ApexPages.StandardSetController(...); opportunityRecords.setPageSize(...); However, I generally recommend that you don't use the standard set controller if you're not using pagination. It would be more efficient to just use a List<Opportunity> instead. – sfdcfox Aug 17 '17 at 17:26
0

ApexPages.StandardSetController ctrl = new ApexPages.StandardSetController(O); //pass Opportunity Instance.

AKController ctrl = new AKController(ctrl);

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.