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
Ryan Chen 1Ryan Chen 1 

Apex Extension "Order By" not sorting OrderItems by UnitPrice in VF Page

Hello,

I'm new to VF & Apex and have been able to get by with most things doing my own research. I'm currently doing a project where I automate an order confirmation page using fields on the order object. 

I know this may be somewhat of a simple problem, but the only roadblock I've come into, is being able to sort the items in my VF table by the unit price. I know of using controller extension SELECT... FROM... ORDER BY... but it doesn't seem to be working. Below is my extension & snippet of the VF table. Could someone please help me out or point me in the right direction for a solution? Thank you so much!

Controller Extension:
public class OrderProductExtension{ 
     public List<OrderItem> getproducts(){
               return products;
     }    
    public OrderProductExtension(ApexPages.StandardController stdController){}
     List<OrderItem> products = 
        [SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem 
         ORDER BY UnitPrice DESC];
}

VF Page Table: 
<apex:pageBlock title="Line Items" >
        <apex:pageBlockTable value="{!Order.OrderItems}" var="oi" >
            <apex:column value="{!oi.Quantity}"/>
            <apex:column value="{!oi.PricebookEntry.Product2.Name}"/>
            <apex:column value="{!oi.UnitPrice}"/>
            <apex:column value="{!oi.TotalPrice}"/>
        </apex:pageBlockTable>
       </apex:pageBlock>
Best Answer chosen by Ryan Chen 1
Hitendar Singh 9Hitendar Singh 9
@ryan
Guess you would like to show only the orderitems related to a particular order. If so, please update with below query,

products = [SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem where orderId =: stdController.getId() ORDER BY UnitPrice DESC];

All Answers

Raj VakatiRaj Vakati
Try this code
 
public class OrderProductExtension{ 
public List<OrderItem>  products {get;set;}
public OrderProductExtension(ApexPages.StandardController stdController){
products = 
[SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem 
 ORDER BY UnitPrice DESC];
 }
}
 
<apex:pageBlock title="Line Items" >
	<apex:pageBlockTable value="{!products}" var="oi" >
		<apex:column value="{!oi.Quantity}"/>
		<apex:column value="{!oi.PricebookEntry.Product2.Name}"/>
		<apex:column value="{!oi.UnitPrice}"/>
		<apex:column value="{!oi.TotalPrice}"/>
	</apex:pageBlockTable>
   </apex:pageBlock>

 
Hitendar Singh 9Hitendar Singh 9
Just replace,
<apex:pageBlockTable value="{!Order.OrderItems}" var="oi" > in your VF page
with 
<apex:pageBlockTable value="{!products}" var="oi" >
Ryan Chen 1Ryan Chen 1
Thanks for the input. I changed the extension and also the VF page but I believe it's now pulling every single OrderItem in the price book and thus I get "Collection size 1,714 exceeds maximum size of 1,000"

My thought that it is because there is no "WHERE condition" that will list only the items I have selected from my price book into the VF page. How would I be able to specify this?
User-added image

 
Raj VakatiRaj Vakati
set page to readonly to true 
 
<apex:page controller="YOUR CLASS " readOnly="true">

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_readonly_context_pagelevel.htm
Hitendar Singh 9Hitendar Singh 9
@ryan
Guess you would like to show only the orderitems related to a particular order. If so, please update with below query,

products = [SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem where orderId =: stdController.getId() ORDER BY UnitPrice DESC];
This was selected as the best answer
Ryan Chen 1Ryan Chen 1
Thank you everyone for the help! It's working now and showing it ordered. Much appreciation for y'all!!!