Check logged in user is belongs to anyone of these Department or not
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 02:33 AM - edited 07-21-2023 03:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 04:27 AM
Hi @Somasekhar6 ,
Hope you are doing great.
Here's the high-level solution in English:
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.
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).
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.
});
}
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 05:02 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2023 07:29 AM - edited 07-21-2023 07:30 AM
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';
}
Regards,
Riya Verma