- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2017 09:56 PM
I've created an advanced User criteria to display a set of cat items only to users who have a certain CONTRACT in their assignment groups. This is my script:
Am I missing something here ? (I tried logging, script executing till the final GlideAggregate 'if' condition)
getFliContract();
function getFliContract() {
var array = [];
answer = false;
var usr = gs.getUser();
var contract = new GlideRecord('u_contract');
contract.addQuery('u_name','Flights);
contract.query();
if (contract.next()) {
var cont_sysid = contract.getValue('sys_id');
var group = new GlideRecord('sys_user_group');
group.addQuery('u_contract',cont_sysid);
group.query();
while(group.next()){
array.push('' +group.sys_id);
}
}
for(var i=0;i<array.length;i++) {
var user = new GlideAggregate('sys_user_grmember');
user.addQuery('user',usr);
user.addQuery('group',array[i]);
user.addAggregate('COUNT');
user.query();
if(user.Next()) {
if(user.getAggregate('COUNT') > 0) {
answer = true;
}else {
answer = false;
}
}
}
return answer;
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2017 03:29 AM
Hi again,
Could you try to hardcode some sys_id's into the variables and then see if it changes anything ? -just to test
If i do this in a fix script in my dev instance as user (sys admin) it works.
answer = (function(){
var usr = gs.getUserID(); //sys admin
var grp = '287ee6fea9fe198100ada7950d0b1b73,cfcbad03d711110050f5edcb9e61038q'; //1 of these groups admin is a member of
gs.print('usr ' + usr);
var user = new GlideRecord('sys_user_grmember');
user.addQuery('user',usr);
user.addQuery('group','IN',grp);
//user.addAggregate('COUNT');
user.query();
var count = 0;
//if(user.next()) {
count = user.getRowCount();
gs.log('count is' +count); //count is 1
if(count > 0) {
gs.log('iteration 5');
return true;
}
})();
gs.log(answer); //returns tru
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2017 10:11 PM
Hi Suhas,
In you code you missed to define the type or answer, i think it should be var answer = false; and in your contract.addQuery('u_name','Flights); you are missing ' after Flights (means there is only one quote). Please correct that and try.
getFliContract();
function getFliContract() {
var array = [];
var answer = false;
var usr = gs.getUser();
var contract = new GlideRecord('u_contract');
contract.addQuery('u_name','Flights');
contract.query();
if (contract.next()) {
var cont_sysid = contract.getValue('sys_id');
var group = new GlideRecord('sys_user_group');
group.addQuery('u_contract',cont_sysid);
group.query();
while(group.next()){
array.push(group.sys_id);
}
}
for(var i=0;i<array.length;i++) {
var user = new GlideAggregate('sys_user_grmember');
user.addQuery('user',usr);
user.addQuery('group',array[i]);
user.addAggregate('COUNT');
user.query();
if(user.Next()) {
if(user.getAggregate('COUNT') > 0) {
answer = true;
}else {
answer = false;
}
}
}
return answer;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2017 10:24 PM
Yep, declared the 'answer' variable & tried...still no luck!
The item is still invisible for all users including those with that particular contract in one of their groups....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2017 10:46 PM
Hi Suhas
Have you tried to log out your answer after its set ?
Is your "COUNT" what you expect ?
Does cont_sysid contain what you expect?
Does array contain what you expect?
If so then you could try using the below as the function call to see if it makes a difference
answer = (function(){
if('everything is ok'){
return true;
}else{
return false;
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-20-2017 11:10 PM
Hi Suhas,
Please give the below script a try and let me know.
getFliContract();
function getFliContract() {
var initial_array = [];
var array = [];
answer = false;
var usr = gs.getUser();
var contract = new GlideRecord('u_contract');
contract.addQuery('u_name','Flights');
contract.query();
if (contract.next()) {
var cont_sysid = contract.getValue('sys_id');
var group = new GlideRecord('sys_user_group');
group.addQuery('u_contract',cont_sysid);
group.query();
while(group.next()){
initial_array.push(group.getValue('sys_id'));
}
array = initial_array.join(",");
}
for(var i=0;i<array.length;i++) {
var user = new GlideAggregate('sys_user_grmember');
user.addQuery('user',usr);
user.addQuery('group',array[i]);
user.addAggregate('COUNT');
user.query();
var count = 0;
if(user.next()) {
count = user.getAggregate('COUNT');
if(count > 0) {
answer = true;
}else {
answer = false;
}
}
}
return answer;
}
I hope this helps.Please mark correct/helpful based on impact