orderByDesc or orderBy does not work in Client Script onChange
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2014 02:42 AM
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);
}
}
}
}
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2014 08:08 AM
Yeah, that's still the client side GlideRecord, even when doing an ajax. I was referring to create a custom script include/ and doing a GlideAjax call to get the data.
Did you try the other solution? sorting ascending, and then using a while loop to loop through the list real quick? I don't know if that would work, or how it might impact response time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2014 08:19 AM
Hi Victoria,
So I tried to go back to the fundamentals on this one and created a script of my own to approximate what you're doing. I did this in a blank Catalog Item on DemoSandbox. First, I created two fields: requested_for and cmdb_ci. Both of them were reference fields, with the first one pointing to sys_user and the second pointing to cmdb_ci. Then I created a client script that looks like this:
(It's an onChange client script that listens to requested_for)
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideRecord("cmdb_ci_computer");
gr.addQuery("assigned_to", newValue);
gr.orderByDesc("sys_created_on");
//gr.orderBy("sys_created_on");
gr.query();
if (gr.next()) {
g_form.setValue("cmdb_ci", gr.sys_id);
}
}
After putting all that together, I tested with both orderBy and orderByDesc and both seemed to work just fine.
So, with that in mind, I think that the problem is someplace else in your script.
In order to find the source of the actual error, I would put in logs/alerts after every line in your original script and see where the error is occurring. I noticed that the error is "undefined is not a function" which suggests that you may be calling something incorrectly. For example, "g_form.setvalue" instead of "g_form.setValue" or something of that sort.
So, in sum: 1) Try adding log statements after each line in your script to find the line where the error is occurring and 2) Pay special attention to all function calls in your script (e.g. myVar.someFunctionCall()) because that seems to be the error that's taking place.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2018 04:58 AM
Hi Vicotia
ServiceNow system does not allow you to sort using date/time field. You are trying orderByDesc("sys_createe_on") which is a date time field. Try to use another field to sort the record.
Bassically in time (12hr format) there can be 1 am as well as 1 pm, so the system cannot differentiate between them. That is why the system does not allow you to sort using date/time field.
There are other fields type as well which system does not sort like image, script, wiki, document etc types of fields.
Hope you got your answer.