Show/hide fields based on logged in user company & assignment group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2023 01:02 AM
Hi Community,
I'm working on a catalog item request with the below requirements.
1) Variable 1 should be visible if user in Company A & Group X
2) Variable 2 should be visible if user is in only Company A & not in Group X
3) Variable 3 should be visible if user is other than Company A
Below is my client script and its not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2023 07:36 AM
1) You need to setMandatory to false before setDisplay to false since mandatory variables or fields cannot be hidden.
2) It sounds like you want your first GlideAjax call to also pass in the newly-selected user value, so add an addParam line like is in your second GlideAjax call.
3) I'm not sure why you are setting display and mandatory on all three variables again after the first GlideAjax return. It seems like this would reverse any action in the validateGroup function.
4) Replace your 'hi3' alert to answer so you can see what, if anything is returned from the CheckGroup Script Include function. Do the same in the getCompCode function.
5) Since variable 1 and variable 2 depend on conditions from both GlideAjax calls, it would be best in each response function to only set the value of a script variable that you declared and initialized at the beginning of the script in the main function. Then, after both GA calls, setDisplay and setMandatory of the variables based on the value of these script variables.
If one or both answer/userco are not the expected value, make sure the Script Includes are Client callable, add some gs.addInfoMessage lines to confirm they are running and to confirm the values passed in from the client, and any logical steps are reached. Post the revised onChange script and Script Includes if you're still not getting the expected return values to the client script response functions. Use the Insert code </> icon to make this easier to read.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2023 10:31 AM
Hi @karthik_540 ,
It seems like there are a couple of issues in your client script that may be causing it not to work as expected. Let's go through the script and make some corrections:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Hide all variables initially
g_form.setDisplay('select_your_need', false);
g_form.setDisplay('select_your_need_1', false);
g_form.setDisplay('select_your_need_2', false);
g_form.setMandatory('select_your_need', false);
g_form.setMandatory('select_your_need_1', false);
g_form.setMandatory('select_your_need_2', false);
// Check user group
var ga = new GlideAjax('CheckUserGroup');
ga.addParam('sysparm_name', 'CheckGroup');
ga.getXML(validateGroup);
// Get user company
var getUDet = new GlideAjax('UserDetails');
getUDet.addParam('sysparm_name', 'getUserCompanyname');
getUDet.addParam('sysparm_user', newValue);
getUDet.getXML(getCompCode);
}
function validateGroup(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer === 'false') {
// User is in Group X
g_form.setDisplay('select_your_need', true);
g_form.setMandatory('select_your_need', true);
}
}
function getCompCode(response) {
var userCo = response.responseXML.documentElement.getAttribute('answer');
if (userCo === 'CompanyA') {
// User is in Company A but not in Group X
g_form.setDisplay('select_your_need_1', true);
g_form.setMandatory('select_your_need_1', true);
} else {
// User is in a company other than Company A
g_form.setDisplay('select_your_need_2', true);
g_form.setMandatory('select_your_need_2', true);
}
}
Here are the changes and explanations:
Initialization of Display and Mandatory: I moved the initial setting of display and mandatory attributes to the top of the onChange function to ensure they are cleared at the beginning of the function.
Synchronous Execution: I added a check for isLoading to ensure that the script doesn't execute during the initial load.
Corrected Variable Naming: I corrected the variable naming in the getCompCode function (userCo instead of userco) to maintain consistency.
Removed Unused Code: Removed unnecessary else condition in the validateGroup function since it doesn't perform any action.
Make sure to replace the script include names and function names with the actual names used in your ServiceNow instance. Also, ensure that the script include functions return the correct values based on your business logic.
Please mark as Accepted Solution & HIT Helpful if it suffice your requirement.
Thanks, Sohith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2023 06:45 AM
Its not working. Priority is taking as company only and executing getCompCode function only
here is my script include for CheckUserGroup