Script to validade if a group in sys_user_group table is Active (true or false).

rafaelalves4337
Tera Contributor

Hello team,

 

I created a Catalog Item form that users can request to activate or deactivate a group, form is completed and the flow is also working fine.

 

the variable type is Reference, like the image below:

Screenshot_166.jpg

 

Depending of user selection (Re-Activate or Deactivate) the requirement is to show an error message below field "Name_of_the_Group": 

"This group is already activated" or "This group is already deactivated".

 

I'm struggling to find a way to make this happen, my scripting is beginner...

Screenshot_167.jpg

 

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

On a server script you could just 'dot-walk' to the active field on the sys_user_group table and test this against the deactivate/re-activate variable value.  In an onChange Catalog Client Script you can do this using Glide Ajax:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

or, since this is a simple case, getReference:

https://docs.servicenow.com/bundle/tokyo-api-reference/page/app-store/dev_portal/API_reference/Glide... 

Both of these are excellent tools to have in your belt, so give them a try sometime.  In this case, you could also use the newer (Utah) Variable Auto-Populate feature by creating a new variable to hold the active/inactive group value, then hide that variable on the form if you'd like.  The advantage here is that your Catalog Client Script would just evaluate this variable's value against the deactivate/re-activate value, without the need for a server call and return in the script!

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc... 

Let me know if you have questions on any of these approaches.

View solution in original post

7 REPLIES 7

You would most likely want to run this onChange of the t/f variable:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
	return;
}

g_form.hideFieldMsg('var_name'); //hide any previous messages
if (newValue == 'true') { //try without the quotes if it doesn't work
    g_form.showFieldMsg('var_name','valid name','info', true);
}
if (newValue == 'false') {
    g_form.showFieldMsg('var_name','invalid name','info', true);
}

Your script is working fine for me, I forgot to mention that there is a selection before that (Deactivate or Re-Activate):

Screenshot_168.jpg

If the user select Deactivate and write a group name that the active status is False, the Show field msg will be 

if (newValue == 'false') {
    g_form.showFieldMsg('name_of_the_group_2','This group is already INACTIVE in Service Now','info', true);

 

If the user select Re-Activate, and the group is already Active, the fieldmsg is:

if (newValue == 'true') { //try without the quotes if it doesn't work
    g_form.showFieldMsg('name_of_the_group_2','This group is already ACTIVE in Service Now','info', true);
 
Screenshot_169.jpg
 
If I user the variable group_status, it show the messages in both selections (Deactivate and Re-Activate), and it need to respect the selection, I don't  want to see the message that a group is already active if the user selected to Deactivate the group.
I tried many things here, nothing is working, so I guess I do not know how to do it.
Sorry for the long text!

It sounds like you just need an AND condition in the IF statements:

if (newValue == 'false' && g_form.getValue('select_the_option_bellow') == 'DEACTIVATE') { //or whatever the actual value is
    g_form.showFieldMsg('name_of_the_group_2','This group is already INACTIVE in Service Now','info', true);