The CreatorCon Call for Content is officially open! Get started here.

dot walking reference fields in gliderecord query

mikemanuel
Kilo Contributor

I'm hoping this is fairly straightforward, but I'm still new to ServiceNow scripting and I'm working with a custom table that was defined for us by a consultant.

The custom table is used for tracking roles that users have been assigned for applications.   I need to build a gliderecord of this table filtered by the applications that are managed by a given support group.   The table, u_application_user_roles, has a reference column named u_application_access which references another custom table named u_application_roles.   The u_application_roles table has a reference field named u_application_access that references Business Service (cmdb_ci_service).   Busines Service has a reference field named assignment_group that references sys_user_group.

I have been able to filter on the business service with this query from a workflow (which also filters on a specified user):

var existing = new GlideRecord('u_application_user_roles');

//existing.addQuery('u_user', current.request.requested_for);

existing.addQuery('u_user', current.variables.caller_id);

existing.addQuery('u_application_access.u_application', current.cat_item.u_business_service);

existing.query();

What I need now is a GlideRecord filtered down to the assignment_group level, but I can't seem to get it right.   Here's what I've tried so far, along with several other configurations:

var existing = new GlideRecord('u_application_user_roles');

existing.addQuery('u_application_access.u_application.assignment_group', 'ea6918f8db19b240f7343220ad961954');

existing.query();

while(existing.next()) {

  gs.print(existing.u_application_access.u_application.name + ',' + existing.u_application_access.u_application.sys_user_group.name);

I'm guessing the addQuery is being ignored as all records in the table are returned.   I can print out the application names, but the assignment group is undefined.

find_real_file.png

Anything that points me in the right direction would be greatly appreciated.

1 ACCEPTED SOLUTION

You will have to query cmdb_ci_service table before using if condition



Please modify your code like below



var existing = new GlideRecord('u_application_user_roles');
existing.addQuery('u_user', 'f48cafabdbacb200f7343220ad9619bb');
existing.query();


while(existing.next()) {
var support = new GlideRecord('cmdb_ci_service');


//support.addQuery();// add query if application for cmdb_ci_service table


support.query();


while(support.next()){
if(support.get(existing.u_application_access.u_application.name)){
  if(support.assignment_group == 'ea6918f8db19b240f7343220ad961954')
    gs.print(existing.u_application_access.u_application.name + ',' + support.assignment_group.name);
}
}


}



Regars,


Sachin


View solution in original post

4 REPLIES 4

sachin_namjoshi
Kilo Patron
Kilo Patron

var existing = new GlideRecord('u_application_user_roles');


//existing.addQuery('u_application_access.u_application.assignment_group', 'ea6918f8db19b240f7343220ad961954');


existing.addEncodedQuery();// your glide record queryt to dt walkt o application table.


existing.query();



while(existing.next()) {


  gs.print(existing.u_application_access.u_application.name + ',' + existing.u_application_access.u_application.sys_user_group.name);



Check below for example.



Dot-Walking within GlideRecord



Regards,


sachin


Thank-you for your response Sachin.   Not sure if I'm completely following the info in the link you provided, but I tried this:



var existing = new GlideRecord('u_application_user_roles');
existing.addQuery('u_user', 'f48cafabdbacb200f7343220ad9619bb');
existing.query();


while(existing.next()) {
var support = new GlideRecord('cmdb_ci_service');
if(support.get(existing.u_application_access.u_application.name)){
  if(support.assignment_group == 'ea6918f8db19b240f7343220ad961954')
    gs.print(existing.u_application_access.u_application.name + ',' + support.assignment_group.name);
}
}



The first GlideRecord returns all records in u_application_user_roles for a specific user, but I suspect the .get function on the second GlideRecord is not returning an instance of cmdb_ci_service since nothing is printed by the print command.   Am I understanding the concept of the post you provided the link to, or am I still missing something?


You will have to query cmdb_ci_service table before using if condition



Please modify your code like below



var existing = new GlideRecord('u_application_user_roles');
existing.addQuery('u_user', 'f48cafabdbacb200f7343220ad9619bb');
existing.query();


while(existing.next()) {
var support = new GlideRecord('cmdb_ci_service');


//support.addQuery();// add query if application for cmdb_ci_service table


support.query();


while(support.next()){
if(support.get(existing.u_application_access.u_application.name)){
  if(support.assignment_group == 'ea6918f8db19b240f7343220ad961954')
    gs.print(existing.u_application_access.u_application.name + ',' + support.assignment_group.name);
}
}


}



Regars,


Sachin


Sachin, Thank-you for your responses.   They were very helpful in resolving my issue.