1

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.

Questions:

  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);
    
    </apex:repeat>
    

example

2 Answers 2

0

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);
7
  • 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
0

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

this.allLineItems=new List<OpportunityLineItem>([select 
id,UnitPrice,Product2.Name,Quantity,Product2.Hasavshevet_product_ID__c 
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) {
        m_names.add(item.Product2.Name);
        m_quan.add(item.Quantity);
        m_price.add(item.UnitPrice);
        if(item.Product2.Hasavshevet_product_ID__c != null)
            m_ids.add(item.Product2.Hasavshevet_product_ID__c);
        else
            m_ids.add(321);
    }

    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]};
        ProductsArr.push(product_row);
    }

You must log in to answer this question.

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