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

This didn't work unfortunately.   I have the business rule as BEFORE and the script as provided - it is still allowing the inactive group to apply.


My thought is the assignment group doesn't exist yet, because no record has been saved/committed correct?   So the gr.addQuery('sys_id',current.assignment_group) part wouldn't work correct?


You are right. The business rule will be triggered only when the form is saved. To do this action at client side, you need to trigger the script via Client script + GlideAjax approach. More info here.


http://wiki.servicenow.com/index.php?title=GlideAjax


The business rule still fires because I am doing BEFORE insert.   I can write a simple script hard coding the group name and it works fine, but I'd rather it confirm based on the active status of the group selected, and not have to hard code it. - to accommodate many inactive groups.



i.e


if (current.assignment_group.getDisplayValue() == "Architect-Support") {


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


}


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


  }