How to get result using query from List field type through GlideRecord?

bbf3562
Kilo Guru

I am struggling to get list of group names based on group type. The "type" column have List type field in sys_user_group table. Type columns can have more than 1 value (Incident, problem, approver, security, etc). I created a script using GlideRecord to print list of group names but it returning empty and I am wondering why.

find_real_file.png

user variable is a sys_id of Type from sys_user_group_type table. I was able to get string value in groupType variable. Now I added another GlideRecord to use groupType variable to get list of group names based on type however I am getting a empty result. Was that has to do with List type field in type column? I was able to test in different query to see if it able to print results in list and it worked but not with getGroup.addQuery('type', 'CONTAINS', groupType);

1 ACCEPTED SOLUTION

Try this and let me know the results. I have added few debugging statements. Please check the system log after running this.


I thought you are sending the user in the user field. Lets see by printing these variables.



else if(option == "grouptype"){


var groupType = '';


var managerName = '';


var list = '';


var groupName = '';


gs.log('+++++++++++++user is '+user);


var getType = new GlideRecord('sys_user_group_type');


getType.addQuery('sys_id', user);


getType.query();



while(getType.next()){


groupType = getType.sys_id+'';


gs.log('++++++++++++++++my group type sys id is ---'+groupType);


var getGroup = new GlideRecord('sys_user_group');


getGroup.addQuery('type', 'CONTAINS', groupType);


getGroup.query();



while(getGroup.next()){


list += getGroup.name + ';\n';


}


gs.log('++++++++++++++++my list is ---'+list);


}


managerEmail = "test";


return list;


managerEmail;


}


View solution in original post

15 REPLIES 15

SanjivMeher
Mega Patron

Your line number 92 needs correction. the '}' should be at line 101. And you can write return list; at line 104 instead of just 'list'.


I just tried that by moving "}" to line 101 and still getting same empty result. I tried different test using different query like getGroup.addQuery('name', 'admin-group'); and was able to print group name. I think it something has to do with getGroup.addQuery('type', 'CONTAINS', groupType);. Type column is a list type field.


find_real_file.png


Can you paste the whole code instead of screenshot.



I see lot of issue in your code.


Line 87 should not be there at all.



line 91 should be groupType = getType.sys_id;



Also below should be changed



Your line number 92 needs correction. the '}' should be at line 101. And you can write return list; at line 104 instead of just 'list'.


Here a copy and paste script,



else if(option == "grouptype"){


var groupType = '';


var managerName = '';


var list = '';


var groupName = '';



var getType = new GlideRecord('sys_user_group_type');


getType.addQuery('sys_id', user);


getType.query();



while(getType.next()){


groupType = getType.name;


}



var getGroup = new GlideRecord('sys_user_group');


getGroup.addQuery('type', 'CONTAINS', groupType);


getGroup.query();



while(getGroup.next()){


list += getGroup.name + ';\n';


}




managerEmail = "test";


list;


managerEmail;


}



user variable come from var user = RP.getParameterValue('sysparm_role'); where it got value from <g:ui_reference> in Jelly.



I don't understand why you said line 87 should not be there at all. That line is where user have sys_id of group type. I use that to get the string name of group type instead of sys_id I can try use that to run in another GlideRecord in sys_user_group table to get list of group names by using groupType variable.



I just tried your way by change code on line 91 and move '}' to line 101 but I end up receive javascript error, "The reference to entity "E" must end with the ';' delimiter."