User criteria not returning value

ramak
Giga Expert

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;

}

1 ACCEPTED SOLUTION

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



  }


View solution in original post

16 REPLIES 16

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;



  }


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;



  }



  //}


  }


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




  1. var user = new GlideRecord('sys_user_grmember');  
  2.   user.addQuery('user',usr);  
  3.   user.addQuery('group','in',array.toString());  
  4.   //user.addAggregate('COUNT');  
  5.   user.query();  
  6.   var count = 0;  
  7.   //if(user.next()) {  
  8.   count = user.getRowCount();  
  9.   gs.log('count is' +count);  
  10.   if(count > 0) {  
  11.   gs.log('iteration 5');  
  12.   answer = true;  
  13.  
  14.   }else {  
  15.   gs.log('iteration 6');  
  16.   answer = false;  
  17.  
  18.   }  

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....


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());