How to Set Default Sort for Service Portal Requests

troyrobson
Tera Contributor

From within the Service Portal, if I click on Requests from the top nav bar and select 'View all requests,' I am taken to the request page that displays my open incidents and requests.   Currently, the 'My Incidents' section sorts by the sys_updated_on field.   I want this list to sort by the sys_created_on field.   I thought I could achieve this by editing the Requests menu item from the 'SP Header Menu' parent menu, but my edits had no effect.   What am I doing wrong or am I approaching this incorrectly?

  • Go to Service Portal > Menus
  • Open menu instance with the Title: SP Header Menu
  • In the Menu Items related list, select Requests

Here are my edits to the Server Script field with the OOB code commented out:

// maximum number of entries in this Menu

var max = 30;

var t = data;   // shortcut

t.items = [];

var u = gs.getUser().getID();

// use record watchers to tell header when to update dropdown counts

t.record_watchers = [];

t.record_watchers.push({'table':'service_task','filter':'active=true^opened_by=' + u});

t.record_watchers.push({'table':'incident','filter':'active=true^caller_id=' + u});

t.record_watchers.push({'table':'sc_request','filter':'active=true^requested_for=' + u});

var st = new GlideRecord('service_task');

if (st.isValid()) {

  st.addActiveQuery();

  st.addQuery('opened_by', gs.getUserID());

  //st.orderByDesc('sys_updated_on');

  st.orderByDesc('sys_created_on');

  st.setLimit(max);

  st.query();

  while (st.next()) {

      var a = {};

      //$sp.getRecordValues(a, st, 'short_description,sys_id,number,sys_updated_on');

      $sp.getRecordValues(a, st, 'short_description,sys_id,number,sys_created_on');

      if (st.short_description.nil())

          a.short_description = "(No description)";

      a.__table = st.getTableName();

      a.type = 'record';

      //a.sortOrder = st.sys_updated_on.getGlideObject().getNumericValue();

      a.sortOrder = st.sys_created_on.getGlideObject().getNumericValue();

      t.items.push(a);

  }

}

var z = new GlideRecord('incident');

z.addActiveQuery();

z.addQuery('caller_id', gs.getUserID());

//z.orderByDesc('sys_updated_on');

z.orderByDesc('sys_created_on');

z.setLimit(max);

z.query();

while (z.next()) {

  var a = {};

  //$sp.getRecordValues(a, z, 'short_description,sys_id,number,sys_updated_on');

  $sp.getRecordValues(a, z, 'short_description,sys_id,number,sys_created_on');

  if (z.short_description.nil())

      a.short_description = "(No description)";

  a.__table = z.getTableName();

  a.type = 'record';

  //a.sortOrder = z.sys_updated_on.getGlideObject().getNumericValue();

  a.sortOrder = z.sys_created_on.getGlideObject().getNumericValue();

  t.items.push(a);

}

var z = new GlideRecord('sc_request');

z.addActiveQuery();

z.addQuery('requested_for', gs.getUserID());

//z.orderByDesc('sys_updated_on');

z.orderByDesc('sys_created_on');

z.setLimit(max);

z.query();

while (z.next()) {

  var ritm = new GlideRecord('sc_req_item');

  ritm.addQuery('request', z.getUniqueValue());

  ritm.query();

  if (!ritm.next())

      continue;

  var a = {};

  //$sp.getRecordValues(a, z, 'sys_id,number,sys_updated_on');

  $sp.getRecordValues(a, z, 'sys_id,number,sys_created_on');

  if (ritm.hasNext())

      a.short_description = ritm.getRowCount() + ' requested items';

  else

      a.short_description = ritm.cat_item.getDisplayValue() || ritm.getDisplayValue("short_description");

  a.__table = z.getTableName();

  a.type = 'request';

  //a.sortOrder = z.sys_updated_on.getGlideObject().getNumericValue();

  a.sortOrder = z.sys_created_on.getGlideObject().getNumericValue();

  t.items.push(a);

}

t.items.sort(function(a, b) {

  return b.sortOrder - a.sortOrder;

});

t.items = t.items.slice(0, max); // only want first 30

t.count = t.items.length;

var link = {title: gs.getMessage('View all requests'), type: 'link', href: '?id=requests', items: []};

t.items.unshift(link); // put 'View all requests' first

1 ACCEPTED SOLUTION

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Hi Troy,



Under Service Portal --> Widget Instances search for your instance name 'My Open Incident' under title:


Screen Shot 2017-09-12 at 10.16.48 PM.png



Try adding "Order by" and "Order Direction" in your widget instance form. Then you can set the order via the form for that instance. You may need to configure form layout to get order by field:


Screen Shot 2017-09-12 at 10.18.20 PM.png



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


View solution in original post

5 REPLIES 5

find_real_file.png