Order By not working in "Run Script"

codedude
Mega Expert

I have 2 lists that will always mirror each other. One of the lists is the OOB approval table, the other is a Custom Approval Table I have made for a specific purpose.

I am trying to run a "Run Script" in the workflow that queries both tables for the current.request. I then am wanting to put both result sets in the same order. I have tried various ways and none work. Orderby, OrderByDesc and both together.

Has anyone else ever run into this problem. Keep in mind that this is in a "Run Script" in a workflow. Are there restrictions I am not aware of?

Here is a snippet:

//grabs approvers from oob approval list

function GetOOBApprovers(request)

{

  var grOOBList = new GlideRecord('sysapproval_approver');

  grOOBList.addQuery('sysapproval', request);

  grOOBList.addQuery('state', 'requested');

  grOOBList.orderBy('approver.name');

  grOOBList.orderByDesc('sys_created_on');

  grOOBList.query();

  while(grOOBList.next())

  {

            gs.info('LOG: ' + grOOBList.approver.name);

  }

}

//grabs approvers from custom approval list

function GetCustomApprovals(request)

{

  var grCustomApproversList = new GlideRecord('u_zone_approvers_list');

  grCustomApproversList.addQuery('u_request', request);

  grCustomApproversList.orderBy('u_approver.name');

  grCustomApproversList.orderByDesc('sys_created_on');

  grCustomApproversList.query();

  while(grCustomApproversList.next())

  {

            gs.info('LOG: ' + grCustomApproversList.u_approver.name);

  }

}

1 ACCEPTED SOLUTION

dvp
Mega Sage
Mega Sage

Can you tell us how are you confirming that records are not sorting by order?



I'm assuming that you checked in the Script log statements module and getting confused with the way the records are logged. If you look closely into the records in sys logs, the created time for all of them will have the same time.



If you would like to see the log statements in order then use an increment in loop and put it in log statement. Here is the updated script



var grOOBList = new GlideRecord('sysapproval_approver');


  grOOBList.addQuery('sysapproval', request);


  grOOBList.addQuery('state', 'requested');


  grOOBList.orderBy('approver.name');



  grOOBList.query();



  var count = 0;


  while(grOOBList.next())


  {


  ++count;


          gs.info('LOG: ' + count + ' ' + grOOBList.approver.name);


  }


View solution in original post

6 REPLIES 6

Yes it will order by the name of approver and it did.. I tested in developer instance and it is working as expected


I was depending on the logs too much. I was trusting the order I was seeing displayed in the logs, my mistake.



Thank you