The CreatorCon Call for Content is officially open! Get started here.

onclick of a checkbox, open the list of active use

Kanhaiya_Verma
Tera Contributor

When a user clicks on a checkbox in the Incident Form, a list of active users should be displayed.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Kanhaiya_Verma 

you can use GlideDialogWindow for this to show the list

create onChange client script and use this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue == "true") {
        var w = new GlideDialogWindow('show_list');
        w.setTitle('Active Users');
        w.setPreference('table', 'sys_user_list');
        w.setPreference('sysparm_view', 'default');
        var query = 'active=true';
        w.setPreference('sysparm_query', query);
        //Open the popup
        w.render();
    }

}

This is how the output will look like

AnkurBawiskar_0-1737388544397.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Siddhesh Jadhav
Kilo Sage
Kilo Sage

Hi @Kanhaiya_Verma ,

 

Steps to Implement

  1. Create a New Field

    • Navigate to System Definition > Tables.
    • Select the Incident table.
    • Add a new field:
      • Type: Checkbox
      • Label: Show Active Users
      • Name: u_show_active_users.
  2. Add a Reference Field for Users

    • Add another field in the Incident table:
      • Type: List (Reference).
      • Label: Active Users.
      • Name: u_active_users.
      • Reference: User [sys_user].
  3. Client Script for Checkbox Behavior

    • Navigate to System Definition > Client Scripts.
    • Create a new Client Script:
      • Name: Show Active Users on Checkbox Click.
      • Table: Incident.
      • Type: OnChange.
      • Field: u_show_active_users.
      • Script:
        (function executeRule(control, oldValue, newValue, isLoading, isTemplate) {
            if (isLoading || newValue == oldValue) {
                return;
            }
        
            if (newValue) {
                // Perform a GlideAjax call to fetch active users
                var ga = new GlideAjax('FetchActiveUsers');
                ga.addParam('sysparm_name', 'getActiveUsers');
                ga.getXMLAnswer(function(response) {
                    var users = JSON.parse(response);
                    g_form.clearOptions('u_active_users'); // Clear existing options
                    users.forEach(function(user) {
                        g_form.addOption('u_active_users', user.sys_id, user.name);
                    });
                });
            } else {
                g_form.clearOptions('u_active_users'); // Clear options if unchecked
            }
        })(g_form.getControl('u_show_active_users'), '', '', false, false);
  4. Script Include for Fetching Active Users

    • Navigate to System Definition > Script Includes.
    • Create a new Script Include:
      • Name: FetchActiveUsers.
      • Accessible from: Client Callable.
      • Script:
        var FetchActiveUsers = Class.create();
        FetchActiveUsers.prototype = {
            initialize: function() {},
        
            getActiveUsers: function() {
                var userList = [];
                var gr = new GlideRecord('sys_user');
                gr.addQuery('active', true);
                gr.query();
                while (gr.next()) {
                    userList.push({
                        sys_id: gr.getValue('sys_id'),
                        name: gr.getValue('name')
                    });
                }
                return JSON.stringify(userList);
            },
        
            type: 'FetchActiveUsers'
        };
  5. Test the Functionality

    • Open the Incident form.
    • Check the Show Active Users checkbox.
    • The Active Users list should populate with active users.

Explanation

  • Client Script: Handles the onChange event for the checkbox to fetch users dynamically using GlideAjax.
  • Script Include: Fetches active users from the sys_user table and returns the data to the client script.
  • Reference Field: Displays the list of active users dynamically on the form.

Best regards,
Siddhesh Jadhav

 

If this solution helps resolve your query, kindly mark my answer as helpful and accepted. Let me know if you need further assistance!

Ankur Bawiskar
Tera Patron
Tera Patron

@Kanhaiya_Verma 

you can use GlideDialogWindow for this to show the list

create onChange client script and use this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue == "true") {
        var w = new GlideDialogWindow('show_list');
        w.setTitle('Active Users');
        w.setPreference('table', 'sys_user_list');
        w.setPreference('sysparm_view', 'default');
        var query = 'active=true';
        w.setPreference('sysparm_query', query);
        //Open the popup
        w.render();
    }

}

This is how the output will look like

AnkurBawiskar_0-1737388544397.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Kanhaiya_Verma 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Kanhaiya_Verma 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader