- 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 11:49 PM
Thanks Amlan & Simon, tried both still the cat item is not available from the self service portal...
Found that the count is always < 0, so it always returns FALSE no matter what
if(user.next()) {
count = user.getAggregate('COUNT');
if(count > 0) { // this never gets executed as the logs inside this loop never gets displayed......
return true;
}else { // only this one gets executed.......always...... !!!!!!
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2017 12:06 AM
Tried a slightly different method, used getRowCount()... but the count is coming up 0 in logs ,still not working
for(var i=0;i<array.length;i++) {
var user = new GlideRecord('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.getRowCount();
gs.log('count is' +count);
if(count > 0) {
gs.log('iteration 5');
answer = true;
}else {
gs.log('iteration 6');
answer = false;
}
//}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2017 12:18 AM
Hi Suhas
Can you confirm that array contains a sys_id of a group that the user is member of ?
if so then you could try with this instead of running it in a for loop:
This way only 1 GR is created
- var user = new GlideRecord('sys_user_grmember');
- user.addQuery('user',usr);
- user.addQuery('group','in',array.toString());
- //user.addAggregate('COUNT');
- user.query();
- var count = 0;
- //if(user.next()) {
- count = user.getRowCount();
- gs.log('count is' +count);
- if(count > 0) {
- gs.log('iteration 5');
- answer = true;
- }else {
- gs.log('iteration 6');
- answer = false;
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2017 12:54 AM
Still no luck, Simon...got the sysID of the group with that contract in logs...just that the count is always coming up 0...not sure why though....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2017 01:16 AM
Hi Suhas,
This might be the error:
var usr = gs.getUser();
Thats the user object and not sys_id
So either change it to gs.getUserID() or use
user.addQuery('user',usr.sys_id.toString());