function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ckellieckellie 

How to create Button to Sort Related Items

I am trying to create a Javascript button to custom sort Quote Line Items by a custom field. All I have is:

doSort({!QuoteLineItem.Quote_Item_Number__c}, asc)

 

I have been trying to find an example of how to do this, but have come up short. Can anyone point me to an example or how to write a button from Javascript?

 

What is another way to achieve the same result?

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
sh-at-youseesh-at-yousee

Hi,

 

I guess it depends on what it is you want to achieve and in what context.

 

If you're looking to add a custom button to a standard page and have that sort quote line items I'm not sure that can be done. My approach would be to either:

 

  1. Create a VisualForce Page where you in your controller/extension simply sort your data in the query (see example below)
  2. Same as above but with the addition that you have an input field (e.g. picklist) where the user selects which field to sort data by and then a button to execute the query
  3. Implement your own sort

Example of sorting data:

 

 

String order = 'CreatedDate DESC';
List<QuoteLineItem> items = [SELECT Id FROM QuoteLineItem ORDER BY :order];

The example above is somewhat prepared for my 2nd suggestion. Assuming your VF Page has an input field and a button which is linked to your controller, you could, in your controller, set the order variable based on the users input, then fetching data again but now ordered by the user's input.

 

 

Finally, if you don't want to have users stress the system with quering the database all the time you could fetch the data once (assuming a default sort based on a field on the User object/profile/role) and then keep data in your controller and simply sorting data in memory based on the user's input (without quering the backend).

 

Hope it helps.

 

/Søren Nødskov Hansen

All Answers

sh-at-youseesh-at-yousee

Hi,

 

I guess it depends on what it is you want to achieve and in what context.

 

If you're looking to add a custom button to a standard page and have that sort quote line items I'm not sure that can be done. My approach would be to either:

 

  1. Create a VisualForce Page where you in your controller/extension simply sort your data in the query (see example below)
  2. Same as above but with the addition that you have an input field (e.g. picklist) where the user selects which field to sort data by and then a button to execute the query
  3. Implement your own sort

Example of sorting data:

 

 

String order = 'CreatedDate DESC';
List<QuoteLineItem> items = [SELECT Id FROM QuoteLineItem ORDER BY :order];

The example above is somewhat prepared for my 2nd suggestion. Assuming your VF Page has an input field and a button which is linked to your controller, you could, in your controller, set the order variable based on the users input, then fetching data again but now ordered by the user's input.

 

 

Finally, if you don't want to have users stress the system with quering the database all the time you could fetch the data once (assuming a default sort based on a field on the User object/profile/role) and then keep data in your controller and simply sorting data in memory based on the user's input (without quering the backend).

 

Hope it helps.

 

/Søren Nødskov Hansen

This was selected as the best answer
ckellieckellie

I think I will use the second choice you wrote. Thank you