Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

orderByDesc or orderBy does not work in Client Script onChange

victoria32
Kilo Explorer

Hello All,

 

I received the error below when tried to modify "u_affected_user" field which leads "cmdb_ci" modifying due to Client Script onChange "Affected User" field:

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

    //If this is a new record then run on load

    if (g_form.isNewRecord()) {

          //If the new value isn't blank

          if(newValue != '') {

                //Type appropriate comment here, and begin script below

g_form.setValue('u_workstation', '');

var gr = new GlideRecord('cmdb_ci_computer');

gr.addQuery('assigned_to', newValue);

gr.orderByDesc('sys_created_on');

gr.query();

if(gr.next()){

g_form.setValue('u_workstation', gr.sys_id);

}

               

          }

    }

    //If the page isn't loading

else if(!isLoading){

if(newValue != '') {

                //Type appropriate comment here, and begin script below

g_form.setValue('u_workstation', '');

var glr = new GlideRecord('cmdb_ci_computer');

glr.addQuery('assigned_to', newValue);

glr.orderByDesc('sys_created_on');

glr.query();

if(glr.next()){

g_form.setValue('u_workstation', glr.sys_id);

}

               

          }

}

}

Error.JPG

I tried wirh just orderBy('sys_created_on'), but SNOW put the oldest when I need the youngest. I recieve this error when trying to create a new Incident and filling fields.

 

Could some one share thoughts, please, on how to sort dates and get the nearest to today to put in ci field?

7 REPLIES 7

Sanjeev Kumar1
Kilo Sage

Hi Victoria,



You need to use tow function


1.   orderBy


after it you should use.


2. orderByDesc



Just like.



var gr = new GlideRecord('incident');


gr.addQuery('active', true);


gr.orderBy('category');


gr.orderByDesc('sys_created_on');


gr.query();




For more details see following



http://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/



Thanks,


Sanjeev Kumar


Hello Sanjeev,



Thank you.



I tried:


gr.orderBy('sys_created_on');


gr.orderByDesc('sys_created_on');


and it did not help.


Chris M3
Tera Guru

I don't know how accurate this is, but the wiki doesn't list a DESC option for client side GlideRecord queries.



http://wiki.servicenow.com/index.php?title=Client_Side_GlideRecord



The query should really be done using an AJAX call so as not to impact responsiveness from the user side.



If you stick with this, I wonder if this would work with a normal order by


while (gr.next()) {


}


g_form.setValue('u_workstation', gr.sys_id); //Should be at the last record


Hello Chris,



The work part of the script now is:


var gr = new GlideRecord('cmdb_ci_computer');


gr.addQuery('assigned_to', newValue);


gr.orderByDesc('sys_created_on');


gr.query(grResponse);


function grResponse(gr) {


if(gr.next()){


g_form.setValue('u_workstation', gr.sys_id);


                              }


                                                                    }



I have the same error.