Conditional Sorting of Models?

  • 1
  • Question
  • Updated 10 months ago
  • Answered
We have 2 different companies using the same Salesforce and separate the data via hierarchies. We have several pages that both companies use on similar objects. However,  company A likes things a bit different than company B. Specifically how things are sorted on page load...

It's a small problem, but I'd like to be able to change the sort from ASC to DESC based on the User Profile that is viewing the page. And possible change the fields that a model is sorted by.

How could I make this happen?

Thank you 
Photo of Sam Becker

Sam Becker

  • 1,742 Points 1k badge 2x thumb

Posted 11 months ago

  • 1
Photo of Mike Dwyer

Mike Dwyer, Champion

  • 4,716 Points 4k badge 2x thumb
Here is one approach:

1. Add (or use) a model on the USER object. (I'll name the model User.)
2. Add a UI-only, formula field we'll name UI_SortPref.
3. The field formula will derive a value of "ASC" or "DESC" from the user profile.
4. Set the model to Query on Page Load.

4. Set your data model(s) to not Query on Page Load. (I'll call it MainData.)
5. Enter the sort field in Field To Order Records By. (I'll use CreatedDate.)

6. Create an Inline Javascript resource (Resource Location is Inline, not Inline (Snippet).)
7. The javascript can look something like this:

(function(skuid){ var $ = skuid.$;
    var d = skuid.model.getModel("MainData");
    var u = skuid.model.getModel("User");
    var p =[0].UI_SortPref;
    d.orderByClause = 'CreatedDate ' + p;

Step 5 is a necessary to create the javascript field orderByClause. Otherwise it is undefined, and the 7th line of the javascript fails.

You can probably extend the code to set more of the ORDER BY for different sort-field requirements.

In my testing, I wrote the UI-only field formula as {{$Param.sort}}, and added &sort=DESC to the URL as an alternative to taking the sort preference from the Uer Profile. Unfortunately, skuid does not (yet) recognize the merge field  in Field To Order Records By. Otherwise, we could simply make that field CreatedDate {{$Param.sort}}. Maybe when skuid finally lets us build the Order By clause from a field selector.

Photo of Matt Davis

Matt Davis, Employee

  • 1,270 Points 1k badge 2x thumb
Hey Sam, if you're on a version of Skuid that has action sequences then you should be able to accomplish this declaratively.

You can create an action sequence that is Event-triggered when the Skuid page is rendered. In that action sequence you can have a branch action so if user is in company A then it runs the sort action to sort the model ASC, and if they aren't then run the sort action to sort it DESC. 

It would look something like the attached image
Photo of Sam Becker

Sam Becker

  • 1,742 Points 1k badge 2x thumb
Thank you both for these answers! I was able to get it working with both.