Addquery multiple fields to reference in script

coreyo
Giga Contributor

I think I am close.   I want to prevent a user from using an (inactive) value/name in the group. (Normally being pulled from their templates).   Here is my script:

var gr = new GlideQuery('groups');

gr.addQuery('name',current.assignment_group.getDisplayValue());

gr.addQuery('active');

gr.query();

if (gr.active == false) {

      gs.addInfoMessage('This group is not active, please update your template accordingly');

      current.assignment_group.setError('This group is not active, please update your template accordingly');

      current.setAbortAction(true);

  }

1 ACCEPTED SOLUTION

My bad. I found the error. It was line no 1


Here is the updated code.


var gr = new GlideRecord('sys_user_group');  


gr.addQuery('sys_id',current.assignment_group);  


gr.addQuery('active', true);  


gr.query();  


if(!gr.next()) {  


      gs.addInfoMessage('This group is not active, please update your template accordingly');  


      current.assignment_group.setError('This group is not active, please update your template accordingly');  


      current.setAbortAction(true);  


  }  



View solution in original post

11 REPLIES 11

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Corey,



GlideQuery should be replaced with GlideRecord. Can you please give more details on what you mean by "I want to prevent a user from using an (inactive) value/name in the group"



No need to use GlideRecord if this check is for the same table. Complete script should be



if (current.active == false) {


      gs.addInfoMessage('This group is not active, please update your template accordingly');


      current.assignment_group.setError('This group is not active, please update your template accordingly');


      current.setAbortAction(true);


  }


In the groups table, for example a group is no longer active (i.e Active = False).   The group is still being applied on the person change form via their template.   On before save, I want the user to be prompted that he/she cannot use group and to use an Active one.



User applies their template, template still has an old group (that is no longer active).   I need the script to lookup the groups table based on users selection and confirm if 'that' group is active or not.



Thank you.


Pradeep - The business rule that I have applying this script is on the change form. - before insert.


Hello Corey,



Thanks for the update. Please create a BEFORE business rule on change table with below script.


var gr = new GlideQuery('sys_user_group');


gr.addQuery('sys_id',current.assignment_group);


gr.addQuery('active', true);


gr.query();


if(!gr.next()) {


      gs.addInfoMessage('This group is not active, please update your template accordingly');


      current.assignment_group.setError('This group is not active, please update your template accordingly');


      current.setAbortAction(true);


  }