+ Start a Discussion
David SomekhDavid Somekh 

VISUALFORCE - Sort Opportunity line items as they are displayed

I have a visual force page that uses the apex:repeat to loop through the Opportunity line items. B

ut the order is not as it displayed in the Opportunity. I

can check the SortOrder value of the line item, but this value is updated only when the user clicks the "Sort" button.

  1. Is there a way to get items as they are displayed in the opp?
  2. If not, can I trigger the SortOrder value to be updated without the sort button?
var ProductsArr = new Array();

<apex:repeat value="{!Opportunity.OpportunityLineItems}" var="lineitem"> var product_row = {name:"{!lineitem.product2.name}", quantity:{!lineitem.quantity}, ID:{!lineitem.product2.Hasavshevet_product_ID__c},price:{!lineitem.UnitPrice}};

console.log("{!lineitem.SortOrder}"); ProductsArr.push(product_row);

Best Answer chosen by David Somekh
Deepali KulshresthaDeepali Kulshrestha
Hi David,

I've gone through your requirement and instead of doing this you can query the product of opportunity in sorted order in VF Controller and 
use the instance in VF Page, see the code below:

Apex Code--->

List<OpportunityLineItem> allLineItems{get;set;}

List<OpportunityLineItem> allLineItems=new List<OpportunityLineItem>([select id,UnitPrice,Product2.Name,Quantity,Product2.Hasavshevet_product_ID__c
                                                                              from OpportunityLineItem  ORDER BY Product2.Name ASC]);

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha