While adding products to an opportunity in Salesforce, you can not specify the values for the 'SortOrder' field on Opportunity Line Items. That is very weird, and is a feature requested more than 7 years ago right now (as I've seen in an Idea Exchange thread).

I have a request that I need to be able to build a page that parses a CSV file, and creates the listed products and create a relationship with a specified opportunity. Parsing is fine, and so is creating all of the objects. However, I can't seem to find a way to sort the products the way I want to.

If the list is like this:

  1. Product C
  2. Product A
  3. Product B

Then I would like them to appear just like that in the opportunity's related list. But Salesforce orders them by the name, so the lists end up like:

  1. Product A
  2. Product B
  3. Product C

The order is relevant in my situation, so I would like to know if it is possible to insert the Opportunity Line Items records in a specific order, so that they end up in the right order. I'm asking this because I've tested my work so far, and I got the products inserted, but not in the right order.

  • 1
    Sorting the order by the order they should appear in should be sufficient. If they're in the correct indices, they should get the correct sort order. – sfdcfox Nov 7 '16 at 23:25
  • You mean by the insertion order? The first element on the list is inserted first, and thus will be the number 1, and the last will be x? – Renato Oliveira Nov 8 '16 at 0:21
  • 1
    Yes, it's been a while since I've done it programmatically, but historically, the order was correct based on the insert order. Feel free to try it out, and let me know if it works. – sfdcfox Nov 8 '16 at 1:49
  • 1
    In addition to the above it would be advised that wherever you need the queried Line Items to be in order you sort them by product name. That way you are assured they are i the right order when you need them. – Eric Nov 8 '16 at 3:02

Your Answer

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

Browse other questions tagged or ask your own question.