The Zurich release has arrived! Interested in new features and functionalities? Click here for more

AM_Guy
Tera Contributor

I couldn't find a good example of how to change the sort order on a related list, so I thought I could post one.

I did it via a Client Script, so it happens after loading the form.   I also had to set the Type to onChange instead of onLoad because the onLoad didn't work.   I would presume it is because the Related List isn't rendered in time for the onLoad Client Script, so the script had nothing to sort.   However, it appears that modifying the onChange script a little accomplishes the same thing.

Anyways, here it is:

Name: Sort by Created

Active: True

Global: True

Type: onChange

Table: {table name}

Field Name: {A field that is present on the form}

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    //only run on load, if I used the onLoad type of client script, it didn't work for some reason.   I would presume it ran before the related list was available.

//You could also remove this IF statement to have the sort order enforced every time the field is changed and onLoad

  if (!isLoading) {

          return;

    }

var relatedList = {tableName}.{relatedTableName}.{fieldName}   //You'll need to set this.   Example: "problem.incident.problem_id"   Here is a resource to help figure this value out: http://www.servicenowguru.com/scripting/client-scripts-scripting/reload-form-related-list-client-scr...


  if(GlideList2.get(relatedList)){//verify the related list exists

  GlideList2.get(relatedList).sortDescending('sys_created_on');//set the sort order on the related list

  }

}

If you know another way, let me know

4 Comments
abdul_qulatein
Giga Expert

Hi Tom,



I tried implementing this to no avail.



I am trying to setGroupBy the task table which is added as related on the Windows Servers (cmdb_ci_win_server) by Task type (sys_class_name).



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (!isLoading) {


          return;


    }



var relatedList = g_form.getTableName().task.cmdb_ci;



if(GlideList2.get(relatedList)) {



GlideList2.get(relatedList).setGroupBy('sys_class_name');//set the sort order on the related list


   


  }



}



I am executing the above client script from cmdb_ci table I have the inherited check box ticked.



Is there anything wrong my logic above?


Slava Savitsky
Giga Sage

Try this:



var relatedList = g_form.getTableName() + '.task.cmdb_ci';



instead of:



var relatedList = g_form.getTableName().task.cmdb_ci;


sannavulla
Kilo Contributor

Hi Slava,



I have tried with sorting on related list and it is working fine but group by on related list is not working. Any idea on this.........


Slava Savitsky
Giga Sage

Unfortunately related lists have limited functionality compared to normal lists. A workaround I find handy is to use "Open new window" option from the related list's context menu. Then the same list opens in a new window where you can group it as necessary. You could also define your own UI Action that would open a grouped list in a new window.