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}};


2 Answers 2


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);
  • if the user did not click the sort button, the value is null. Nov 6, 2019 at 14:20
  • @DavidSomekh Good point, I'll edit in a fix.
    – sfdcfox
    Nov 6, 2019 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. Nov 6, 2019 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, 2019 at 16:25
  • 1
    @DavidSomekh Try sorting by Id, then?
    – sfdcfox
    Nov 6, 2019 at 16:35

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]};

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .