how to orderBy a field ,which a reference table's field

Jiajing Wang
Tera Contributor

I want to query incident table,how to orderBy by reference table(user)'s field?

var gr = new GlideRecord('incident');
gr.orderBy('user.name');// user.name gr.query(); while(gr.next()){ gs.info(gr.number); }
3 REPLIES 3

Chandu Telu
Tera Guru
Tera Guru

Hi 

Check the below link it have an answer for you

https://community.servicenow.com/community?id=community_question&sys_id=45375dcbdb174090d82ffb2439961917

 

Thanks
Chandu Telu
Please Mark ✅ Correct/helpful, if applicable,

Community Alums
Not applicable

Hello @Jiajing Wang 

I don't think so, orderby will work on dot walking fields. Below thread has mentioned workaround. Could you please check if it helps.

https://community.servicenow.com/community?id=community_question&sys_id=c3042ffddb9a33c0a39a0b55ca96...

 

Regards,

Akshay 

Anil Lande
Kilo Patron

Hi,

Yes, it is not possible to apply OrderBy on dodt walked fields in case of reference field. For reference fields default orderBy is applied on display value.

You are applying wrong query here, there is no field with name 'user' on incident table.

If you apply OrderBy on reference fields then it sort it based on display value of reference table. eg. If you apply OrderBy on caller_id then it sort it based on Name of Caller.

var gr = new GlideRecord('incident');
gr.orderBy('caller_id');// user.name is invalid
gr.query();
while(gr.next()){
gs.info(gr.number);
}

 

Thanks,

Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande