Check logged in user is belongs to anyone of these Department or not

Somasekhar6
Tera Contributor

Hi All I have these departments starting with IOT I need to check whether the logged-in user belongs to these departments or not if he belongs to any one of these departments we return the value true others wise false how we can achieve with client script and script include. 

 

 

7 REPLIES 7

Riya Verma
Kilo Sage
Kilo Sage

 Hi @Somasekhar6 ,

 

Hope you are doing great.

 

Here's the high-level solution in English:

  1. Client Script (Client-side):

    • In the client script, use the GlideAjax API to call the server-side script include and pass the necessary parameters. Based on the response from the server-side script include, update a boolean variable (e.g., "userBelongsToIOTDepartments") in the client script.
  2. Script Include (Server-side):

    • Define a function in the script include that takes the user's department as a parameter.
    • Inside the function, query the "sys_user" table to check if the user's department matches any of the departments starting with "IOT."
    • Return the result (true or false).
  3. Once the client script receives the response from the server-side script include, it will update the "userBelongsToIOTDepartments" variable accordingly.

 

function checkUserBelongsToIOTDepartments() {
    var userBelongsToIOTDepartments = false;

    var ga = new GlideAjax('ScriptIncludeName'); // Replace 'ScriptIncludeName' with the actual name of the script include.
    ga.addParam('sysparm_name', 'checkUserIOTDepartments');
    ga.addParam('sysparm_user_department', g_form.getValue('department')); // Pass the current user's department value.

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        userBelongsToIOTDepartments = (answer === 'true');
        // Use the userBelongsToIOTDepartments variable for further actions.
    });
}

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

@Riya Verma  Thanks for the solution but mai i know what is the script include logic to find the user belongs to IOT departments or not

Hi @Somasekhar6 ,

 

Hope you are doing great.

 

Below is reference logic you can use to check if user is belonging to IOT department:

 

 

    checkUserIOTDepartments: function(userDepartment) {
        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('department', 'STARTSWITH', 'IOT');
        grUser.addQuery('department', userDepartment);
        grUser.query();

        return grUser.hasNext() ? 'true' : 'false';
    }

 

 

 

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma