Specifying default sort field / order for List View

Hi,
Is there a way to specify the default sort field and order for list views. I’m looking for a definition that can be placed in custom vardef, listviewdefs or searchdefs files. Whilst the order is (mostly) remembered, if the ‘Clear Search’ button is clicked prior to a new search it always returns to the system default: date_entered rather than my chosen default.

There seems to be the ability to specify the sort_by and sort_order for subpanels, but nothing for listview, so it seems strange that there isn’t something similar in list view. Following much googling I’ve also tried changing the $_REQUEST using a custom view.list.php file, but this doesn’t work either.

Thanks for your help,
Dominic

Current Version: 7.6.4

Hello :slight_smile:

I hope I have understood correctly

You are looking to add a default sort inside list view of a module

For example - Contacts

Using 7.9.4

In every module you now have filters

Inside filters there is basic and advanced filter

Click on advanced

At the bottom you will see “save search” - if you save a search you will see “my filters” when next inside the list view

This helps users quickly change between saved filters

Inside the advanced search you can select the Order column by and if it is ascending or descending - see screen shot

I have tried this Advanced Filter in my Support Tickets, with Ascending, by Created Date, and I get the ones at the top that were created a month ago, not the ones created today. :frowning:

If you want to respect user preferences then use the following code to apply default sorting order:

file location: custom/modules/<module_name>/views/view.list.php

    public function preDisplay()
    {
       $userPreferenceOrder = $GLOBALS['current_user']->getPreference('listviewOrder', 'AOK_KnowledgeBase2_AOK_KNOWLEDGEBASE'); // you can get required category name from "user_preferences" table. 

           if(empty($userPreferenceOrder['orderBy']))
	        {
		      $_REQUEST['orderBy'] = 'field_name';
		      $_REQUEST['sortOrder'] = 'DESC';
	         }

	         parent::preDisplay();
        }

this doesn’t work for me
can you please specify this in detail

@likhithr try looking at the instructions again - I edited the post to show more technical details, the part saying <module_name> was not being shown.

tried this , still doesn’t work

wanna sort the list view on Opportunities to date created descending order by default

Ok, then you need to investigate.

Check your logs for any errors.

Check that you used the correct internal module_name (you should see it in other directory names under custom/modules or under modules.

Run a quick Repair and Rebuild.

Check if your code is getting picked and running, by logging something from inside that function.

Debug it with a debugger

etc

I’m not 100% sure but in 8.x preDisplay doesn’t hit

Try to create public/legacy/custom/modules/Opportunities/Opportunity.php
and then extend Opportunity bean

class CustomOpportunity extends Opportunity
{
	function create_new_list_query($order_by, $where,$filter=array(),$params=array(), $show_deleted = 0,$join_type='', $return_array = false, $parentbean = null, $singleSelect = false,  $ifListForExport = false){
		$ret_array = parent::create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, true, $parentbean, $singleSelect,  $ifListForExport);
		
		if (isset($ret_array['order_by']) and strlen($ret_array['order_by'])){
			
		}
		else{
			$ret_array['order_by'] = ' ORDER BY myfield DESC';
		}
		
        if ($return_array) {
            return $ret_array;
        }
	}
	
}

then tell to suitecrm that opportunity bean point different file
Create CustomOpportunity.php in public/legacy/custom/Extension/application/Ext/Include/

$objectList['Opportunities'] = 'Opportunity';
$beanFiles['CustomOpportunity'] = 'custom/modules/Opportunities/Opportunity.php';
$beanList['Opportunities'] = 'CustomOpportunity';

tried this , doesn’t reflect in descending order(date created) for Opportunity list view

Have you run a quick Repair and Rebuild?
Are you run suitecrm 8.x?

I copied this code from one of my production environments (8.x) and it work

tried doing that,
the behavior is that for few accounts the default sort works, for others it doesn’t

any possible fix?

May be some accounts has order_by set by gui and some others don’t

Check the code, owerwrite hit only if

$ret_array['order_by']

is empty

Try to reset user preferences

can you please share some of your code for reference

I already share it…

if (isset($ret_array[‘order_by’]) and strlen($ret_array[‘order_by’])){
$ret_array[‘order_by’] = ’ ORDER BY date_enteredDESC’;
}
else{
$ret_array[‘order_by’] = ’ ORDER BY date_enteredDESC’;
}

before it was not hitting the else part so, just to check i put the same inside on the if part as well, it passes there, but not on the else part

this makes is only date_entered to be desc permanent, even if i try to change sort for name or other fields it does not take

if "else " part do not hit, mean that “order_by” preference was set before, maybe in user preferences? Try to reset user preferences

how do I reset every time when page refreshes?

You can’t, you asked about default order, but if users customize order by (click on column arrows), default order (“else” in the code) doesn’t hit