function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sfdc WolfSfdc Wolf 

Sorting the record list by field in Opportunity Products and Quote Line Items related lists

We all know that there is a sort by field option in Page layout for all most all the related lists except Opportunity Products and Quote Line Items. I need to sort the line items by line number for both Opportunity products and Quote Line Items.That's my first question.

FYI, I tried [ order by 'custom field' asc ] via SOQL Query already, but the sorting is only done depending upon product name.

Like we have list.sort() method, do we have any list.sortby(field)method ?

I also have service type line items created programatically for each product which I have customized already, but I need to sort and display each product followed by its service type line item. any suggestions are much appreciated !! TIA
JeffreyStevensJeffreyStevens
Well, if you're doing this in APEx, you could put your data in a list of wrapper classes, and then sort based on that.
Sort wrapper classes
Add “implements Comparable” to the class definition.  (ie global class className implements comparable {} )
Add a method that looks like this…
                  Public integer compareTo(Object compareTo) {
                     wrapperClassName compareToXXX = (wrapperClassName)compareTo;
                     Integer returnValue = -1;
                     If (sortFieldNameFromWrapperClass == compareToXXX.sortFieldNameFromWrapperClass) returnValue = 0;
                     If (sortFieldNameFromWrapperClass > compareToXXX.sortFieldNameFromWrapperClass) returnValue = 1;
                     Return returnValue;
                  }


here is some actual code (apvreated)...
//
    //	displayLine class
    //
    public class displayLine implements Comparable{
    	public id		agentId				{get;set;}
        public string	agentName		  	{get;set;}
    	public id		accountId	  		{get;set;}
    	public string	accountName			{get;set;}

    	
    	// Constructor
    	public displayLine(
    		id		tmpAgentId,
    		string	tmpAgentName,
    		id		tmpAccountId,
    		string	tmpAccountName
    	)
    	
    	// Assign properties of sub-class
    	{
    		agentId				= tmpAgentId;
            agentName		  	= tmpAgentName;
    		accountId		  	= tmpAccountId;
    		accountName 	   	= tmpAccountName;
    	}
    	
    	public integer compareTo(object compareTo){
    		displayLine compareToDL = (displayLine)compareTo;
    		integer returnValue = -1;
    		if (accountName == compareToDL.accountName) returnValue = 0;
    		if (accountName > compareToDL.accountName) returnValue = 1;
    		return returnValue;
    	}