stratoflow

Displaying table with fields from different object and fast columns sorting

It is not uncommon to stumble across some problems when you want to display a table containing fields from various different objects.

In order to do that properly, you should make use of Wrapper Class.

Wrapper Class

Wrapper Class usually takes the form of an inner class inside the controller and combines all the data retrieved from the database.

Having a table with fields from only one object seems way easier, than putting multiple objects inside.

And quite frankly it is.

Building a Salesforce application or extending your development team?

🚀 We're here to assist you in accelerating and scaling your business. Send us your inquiry, and we'll schedule a free estimation call.

Estimate your project

Sorting

With that settled, we can now move on to sorting.

First of all, we need our wrapper class to implement a Comparable interface. The interface is only natural to use in order to compare two custom objects with each other, based on different criteria.

An example of such wrapper class would look like this:

public class OrdersB2BPageObject implements Comparable {
public String orderNumber {
get;
set;
}
public String reference {
get;
set;
}
public OrdersB2BPageObject(String orderNumber, String reference) {
this.orderNumber = orderNumber;
this.reference = reference;
}
public Integer compareTo(Object compare) {
OrdersB2BPageObject compareTo = (OrdersB2BPageObject) compare;
if (OrdersB2BPageController.sortBy.equals('OrderNumber')) {
return -orderNumber.compareTo(compareTo.orderNumber);
} else if (OrdersB2BPageController.sortBy.equals('Reference')) {
if (reference == null && compareTo.reference != null) {
return -1;
} else if (reference != null && compareTo.reference == null) {
return 1;
} else if (reference == null && compareTo.reference == null) {
return 0;
}
return reference.compareTo(compareTo.reference);
}
return null;
}

In this case, orderNumber is a field from Order object and reference can be a field from any other object.

Having the comparator for all of the fields, we need to use it in order to sort columns. 

For that purpose, our controller will hold the attribute “sortBy” which will hold the information about which column was clicked in order to be sorted.

You may notice that this variable is used in the “compareTo” method inside the wrapper class.

We also need a method that is fired after the column click, which looks like that:

public PageReference sortColumn() {
ordersB2BPageObjects.sort();
orderListIterable = new OrderB2BListIterable(ordersB2BPageObjects);
next();
return null;
}

And finally let us move on to the Visualforce page:

<apex:page controller="OrdersB2BPageController">

<apex:pageBlock id="ThePage">
<apex:form >
<apex:pageBlockTable value="{!listController}" var="orderB2B">
<apex:column >
<apex:facet name="header">
<apex:commandLink value="{!$Label.Community_orderNumber}" action="{!sortColumn}" reRender="ThePage">
<apex:param name="compareField" value="OrderNumber" assignTo="{!sortBy}"/>
</apex:commandLink>
</apex:facet>
<apex:commandLink value="{!orderB2B.orderNumber}" action="{!getSelectedOrder}" reRender="OrderItems">
<apex:param name="orderNumber" value="{!orderB2B.orderNumber}"
assignTo="{!selectedOrderNumber}"/>
</apex:commandLink>
</apex:column>

<apex:column value="{!orderB2B.reference}">
<apex:facet name="header">
<apex:commandLink value="{!$Label.Community_reference}" action="{!sortColumn}" reRender="ThePage">
<apex:param name="compareField" value="Reference" assignTo="{!sortBy}"/>
</apex:commandLink>
</apex:facet>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>

</apex:page>

And just as a brief explanation:

the main part for our sorting is everything inside the apex:facet markup.

We create an apex:commandLink out of the columns header which fires !sortColumn action and Rerenders whole page after that.

Additionally to the click, using apex:param we assign the value of the corresponding column to the “sortBy” attribute from the controller.

I hope this will clarify all the uncertainties and will help you out in your work!

Building a Salesforce application or extending your development team?

🚀 We're here to assist you in accelerating and scaling your business. Send us your inquiry, and we'll schedule a free estimation call.

Estimate your project

Testimonials

The developed software product was built from scratch with solid quality. We have had a long-term engagement with Stratoflow for nearly 10 years. We look at them as partners, rather than contractors. I'm impressed by their team culture and cross-team support.

Nathan Pesin

CTO, Legerity Financials

Stratoflow was a great partner, challenging as well as supporting our customer projects for the best outcome. They have a great pool of talent within the business - all very capability technologists, as well as being business-savvy and suitable for consultancy engagements.

Chris Goodall

Managing Consultant, CG Consultancy (UK) Limited

The bespoke metal exchange platform works great, it is easily accessible and richly functional. Stratoflow managed deadlines capably, meticulously documented their progress, and delivered a complex project at an affordable cost.

Bartlomiej Knichnicki

Vice Chairman, Supervisory Board

We are very pleased with our partnership with Stratoflow and, as we continue to grow, we expect to increase the numbers of developers that work with us on our projects. They have proven to be very skilled and flexible. They're extremely reliable, and they have a very good company culture of their own, which gives them a real edge compared to other providers that serve more as production shops rather than thought partners and creative problem solvers.

Andrew Kennedy

Founder & Managing Director, Tier 2 Consulting

Stratoflow successfully customized the system according to the specific functionalities and without bugs reported. The team was commended for their adaptability in the work process and for their responsiveness.

Joshua Blavins

Tech PM, Digital Agency

The features implemented have received overwhelmingly positive feedback from end-users. Stratoflow has an incredible technical expertise and a high degree of flexibility when it comes to changing project requirements.

Adam Hill

Chief Technology Officer, Legerity

They have impressively good knowledge of AI issues. Very responsive to any amendments and findings. Very good communication. We received a finished project which could be implemented into production shortly after testing.

CO-Founder & CTO

Circular Fashion Company

They provided superb service with seamless communication and a highly professional, technical approach. The team displays impressive technical expertise and are willing to share information and engage in constructive feedback.

Filip Stachnik

Operations Manager, Otwarte Klatki (part of Anima International)

They're very skilled technically and are also able to see the bigger picture. Stratoflow can actually think about solutions, not just the technical task at hand, which they've been assigned.

Arnd Jan Prause

Chief Operating Officer, musQueteer

Stratoflow delivered the website successfully within the timeframe and budget. They assured that the output met the set requirements. Overall, the team's performance was excellent and recommended for their exceptional technical business expertise. They've been able to deliver all of their work on time and within budget, which has been very impressive.

Lars Andersen

Founder & CEO, My Nametags

Travel sector rebound after the pandemic is complete. We have fantastic global coverage of travel data distribution due to mutual agreements and data exchange between aggregators. Competition for the best price of limited resources degradates margins.

How to win? Provide personalized experience and build your own products in the front-office. The missing bits: a traveller golden record collecting past activities and a AI/ML recommendation technology.

Michał Głomba

CEO at Stratoflow