0

I have custom related lists in a Visualforce component where each of them needs a pagination method. I found many solutions for pagination, but pagination when it is about one object. But what about a single pagination class or pagination methods for more than one object?. Say, I need a pagination for my custom related list A (Object A) and my custom related list B (Object B).

I have the following apex por pagination:

public void fetch()
{

   String optyquery = 'SELECT Id, Name FROM Opportunity';
   optyquery += ' LIMIT ' + blockSize + ' OFFSET ' + index;
   optylist = Database.Query(optyquery);


    public void beginning()
    {
      index = 0;
      fetch();
    }

    public void previous()
    {
       index = index - blockSize;
       fetch();
    }

    public void next()
    {
       System.debug('##next!');
       index = index + blockSize;
       fetch();
    }

    public void end()
    {
       Integer mod = math.mod(totalRecs,blockSize);
       if (mod != 0) {
          index = totalrecs - math.mod(totalRecs,blockSize);
       } else {
          index = totalrecs - blockSize;
       }
        fetch();
    }

    public boolean getprev()
    {
       if(index == 0)
       return true;
       else
       return false;
    }

    public boolean getnxt()
    {
       if((index + blockSize) >= totalRecs)
       return true;
       else
       return false;
    }

Is there a way to create one universal pagination class for my two objects so I don't have to write as many pagination classes as objects I have.

Any help would be appreciated.


Thanks Adrian for your tips. You are still dealing with only one object in your examples:

setCon = new ApexPages.StandardSetController(Database.getQueryLocator(query));  

and "query" is referring to the object Opportunity:

String query='Select Id,Name, account.name, stagename, ForecastCategoryName FROM Opportunity WHERE Name LIKE \'%' + searchString + '%\' order by Name LIMIT 1000';

if you want this not only for the object Opportunity, but supposing you have two custom related lists both with pagination. Suppose the another object is Contact.

How would you transform your class?.

Another point: I have read in the following link:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_pages_standardsetcontroller.htm

The maximum record limit for StandardSetController is 10,000 records. Instantiating StandardSetController using a query locator returning more than 10,000 records causes a LimitException to be thrown. However, instantiating StandardSetController with a list of more than 10,000 records doesn’t throw an exception, and instead truncates the records to the limit.

If I have more than 10,000 records this will cause a crash. Is that so?

3

Just use the ApexPages.StandardSetController here. This use case is exactly what it was designed for. There is no reason to reinvent the wheel.

public ApexPages.StandardSetController children
{
    get
    {
        if (children == null)
        { // lazy load pattern
            children = new ApexPages.StandardSetController([/*query*/]);
        }
        return children;
    }
    private set;
}

Then in your page you can iterate children.records, check children.hasNext and children.hasPrevious, and call children.first, children.last, children.next, children.previous, and so on.

If you have more child relationships to paginate, you would add more lazy loaded properties. Descriptive naming will be really helpful for you to keep track of them. I would use the relationship name, e.g. Opportunities, Contacts, etc.

public List<Opportunity> opportunities
{
    /*lazy load*/
}
public List<Contact> contacts
{
    /*lazy load*/
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy