-1

Everything below are custom objects.

I selected the Opportunity Line Items in the order of Oli1, Oli2 and then Oli3.

enter image description here

Then I click on DMail button, and I see the table is populated in a different order than what I selected.

enter image description here

I was expecting the table to be populated with Oli1, Oli2 and then Oli3.

VF

<apex:page standardController="opportunity_line_item__c" recordSetVar="records" extensions="Dmail" showHeader="false" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Send Status Update">
          
            <br/><br/><apex:inputTextarea value="{!emailBody}" Style="width:800px;" richText="true" required="false"/><br/>
            
         </apex:pageBlock>
    </apex:form>       
</apex:page>

Apex

public class Dmail 
{
    public ApexPages.StandardSetController standardController;
    public String emailBody {get;set;}
    public List<opportunity_line_item__c> l_opp {get;set;}
    public opportunity_line_item__c opp {get;set;}
    
    
    public Dmail(ApexPages.StandardSetController standardController)
    {
        this.standardController = standardController;
        
        l_opp = new List<opportunity_line_item__c>();
        
        for (opportunity_line_item__c sel : (List<opportunity_line_item__c>)standardController.getSelected())
        {
            opp = [select Name, opportunity__c, Quantity__c, Sales_Price__c, Id from opportunity_line_item__c where Id=:sel.Id];
            l_opp.add(opp);   
        }
        
        for (opportunity_line_item__c sel : l_opp)
        {            
           emailBody = '<br/><br/><table border="1" style="border-collapse: collapse" cellspacing="20" cellpadding="20"><tr><th>Name</th><th>Quantity</th><th>Salesprice</th></tr>';
           for (Integer j = 0; j<l_opp.size(); j++)
           {
               emailBody += '<tr><td>'+l_opp[j].Name+'</td><td>'+l_opp[j].Quantity__c+'</td><td>'+l_opp[j].Quantity__c+'</td></tr><br/>';           
           }
           
           emailBody += '</table><br/>';

        }
    }
}

My main aim is to get the opportunity line items in the order which I selected in my VF page. What exact code changes can be done to achieve it.

1
  • Why are you using a custom object shadowing a standard one?
    – Adrian Larson
    Sep 18, 2022 at 20:23

1 Answer 1

1

StandardSetController.getSelected() does not guarantee any particular order as to the order of selections. You would need to write a query to get the records in the order you want, such as:

Set<Id> lineItemIds = new Set<Id>();
for(sObject sel: standardController.getSelected()) {
  lineItemIds.add(sel.Id);
}
l_opp = [
  SELECT Name, Opportunity__c, Quantity__c, Sales_Price__c
  FROM Opportunity_Line_Item__c
  WHERE Id = :lineItemIds
  ORDER BY Name ASC
];

Side note: do not put a query inside of a for-loop, as this can quickly exhaust governor limits.

Unfortunately, if you want the order to be the same as which the user checked the boxes, you'll need some JavaScript and a completely different method in order to track the selection order.

You must log in to answer this question.

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