Advanced Script for User Criteria

tim_harris
Giga Expert

I am trying to use an advanced script for User Criteria. On the wiki the only documentation says that the script field is "A script to define any additional criteria, and return true or false. This field is available only if Advanced is selected."

There is no example provided. I can't figure out how to reference the user record. For example, if I want to say all users who are active. How would I do this? I know the basic logic, but am unsure of how to reference the user record in the script.

Example logic:

if (currentUserRecord.active == true) {

      return true;

} else {

      return false;

}

Any help would be appreciated.

Thanks,

Tim

1 ACCEPTED SOLUTION

ahaz86
Mega Guru

take a look at this page http://www.servicenowguru.com/scripting/user-object-cheat-sheet/



gs.getUser().getRecord().getValue('active') == true


View solution in original post

11 REPLIES 11

Wrapping it in a function worked. Here is the final code:



scriptTest();


function scriptTest() {


  var retVal;


  if (gs.getUser().getRecord().getDisplayValue('department') == 'Product Management') {


          retVal = true;


  } else {


          retVal = false;


  }


  return retVal;


}


can you try how you initially had it with just


if (gs.getUser().getRecord().getDisplayValue('department') == 'Product Management') {


  return true;


} else {


  return false;


}


I had already tried that and I got the same error message but referencing line 2. I figured it out after cwilker10 responded above. I had to wrap the code in a function to get the return to work correctly. Here is my final working code:


scriptTest();


function scriptTest() {


  var retVal;


  if (gs.getUser().getRecord().getDisplayValue('department') == 'Product Management') {


          retVal = true;


  } else {


          retVal = false;


  }


  return retVal;


}


If all you're interested in is if the user is in the Product Mgmt department, why'd you use the script ?


Why not use the department reference ?



Just askin


zica
Giga Guru

HEllo all, hello



I don't know if this thread is already closed or not but sometimes it is better to use the global variable "answer" within the javascript into. Servicenow and set the 'answer = true / false' depeding on the logic