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

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.

rafaelalves4337
Tera Contributor

Thank you Brad, I used the Auto-Populate feature and it work just perfect!!

That's great to hear, and you are welcome!

Brad, do you know a script to use on the catalog client script that will show a field message like "valid name" or "Invalid Name" if a value of a variable is True or false?