Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Let me try the whole script in my Dev instance...



On 21 Jun 2017 8:30 PM, "simonchristensen" <


Atlast the user criteria is working, this is the updated script :



getFliContract();



function getFliContract(){



  var array = [];


  var answer = false;


  var usr = gs.getUserID(); //get the sysID of the logged in user


  var contract = new GlideRecord('u_contract');


  contract.addQuery('u_name','Flights'); //search the 'u_contract' table for "Flights" contract


  contract.query();


  if (contract.next()) {


  var cont_sysid = contract.getValue('sys_id'); //store the sysID of the "Flights" contract


  var group = new GlideRecord('sys_user_group');


  group.addQuery('u_contract',cont_sysid);   //query the above sysID against the group table


  group.query();


  while(group.next()){


  array.push('' +group.sys_id); //push all the groups with the "Flights" contract to an array


  }


  }



  for(var i=0;i<array.length;i++) {


  var user = new GlideRecord('sys_user_grmember');


  user.addQuery('user',usr);


  user.addQuery('group','IN',array[i].toString());   //check the user group(s) against the array of groups with "Flights" contract


  user.query();


  var count = 0;


  count = user.getRowCount();


  if(count > 0) {


  answer = true;


  }


  return answer; //if any of the user groups have the specified contract, make the Catalog Item available for the logged in user


  }


}



Thanks everyone for your help