I have a visual force page that uses the apex:repeat to loop through the Opportunity line items. But 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}};



The SortOrder field is available, just not updateable. To get the sort order, include it in your script:

var product_row  = {sortOrder: {!BLANKVALUE(lineItem.SortOrder,0)}, name:"{!lineitem.product2.name}", quantity:{!lineitem.quantity}, ID:{!lineitem.product2.Hasavshevet_product_ID__c},price:{!lineitem.UnitPrice}};

Which you can then sort when you're done:

ProductsArr.sort((a,b) => a.sortOrder-b.sortOrder);
| improve this answer | |
  • if the user did not click the sort button, the value is null. – David Somekh Nov 6 '19 at 14:20
  • @DavidSomekh Good point, I'll edit in a fix. – sfdcfox Nov 6 '19 at 14:22
  • thx - but what i want is that if the user did not click "sort", i will get the order as displayed in the UI. – David Somekh Nov 6 '19 at 14:26
  • @DavidSomekh The sort order should be, I believe, by order of creation date, which is the same output I'd expect to see in apex:repeat. If it's not coming out that way, try adding CreatedDate to your object and sorting by that, instead. – sfdcfox Nov 6 '19 at 16:25
  • Tried the approach with the created date, but it does not always help as multiple line items can be added at the same time. Also an opp can be cloned, and then all the line items have the same datetime. – David Somekh Nov 6 '19 at 16:34

In the end, the only solution was to get the products with an APEX controller.

this.allLineItems=new List<OpportunityLineItem>([select 
from OpportunityLineItem where OpportunityId = :Opp.id ORDER BY SortOrder]);  

this.userinput = this.allLineItems[0].Product2.Name;

    m_names = new List<String>();
    m_ids = new List<Decimal>();
    m_price = new List<Decimal>();
    m_quan = new List<Decimal>();

    for (OpportunityLineItem item : this.allLineItems) {
        if(item.Product2.Hasavshevet_product_ID__c != null)

    JSON_NAMES = JSON.serialize(m_names);

These 4 arrays were initlized by the apex controller

public List<Decimal> m_quan    {get;set;}
public List<Decimal> m_price    {get;set;}
public List<Decimal> m_ids    {get;set;}
public String JSON_NAMES {get;set;}

And were later retrieved by the VF page:

for (var index = 0; index < arrayLength;index++) 
        var product_row  = {name:{!JSON_NAMES}[index], quantity:{!m_quan}[index], ID:{!m_ids}[index],price: {!m_price}[index]};
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.