- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 08:13 AM
Hi to all
Please can someone help me with my script
I want to make a script depending on the user role itil to set or not disable the Requested for
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Get the current user's roles
var userRoles = g_user.roles;
// Check if the user has the "itil" role
var hasITILRole = userRoles.indexOf('itil') !== -1;
// Get the requested variable field element by its name
var requestedVarElement = g_form.getControl('dvt_requested_for'); // Replace with the correct name of the requested variable field
// Enable or disable the requested variable field based on the ITIL role
if (hasITILRole) {
requestedVarElement.disabled = true; // Disable the field
// Set the user's display name to the requested variable
var userDisplayName = g_user.displayName;
g_form.setValue('dvt_requested_for', userDisplayName);
} else {
requestedVarElement.disabled = false; // Enable the field
}
}
//g_form.setDisabled ('dvt_requested_for',true);
//javascript:gs.getUserDisplayName();
no itil
itil
Thanks for return
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 01:17 AM
Here is the solution.
So I added to my 'Requested for' variable a default value which is a js code like this -> javascript:gs.getUserDisplayName();
Then instead of a client script, I created a UI Policy by choosing Run script and I put the following code ( @Mike Patel modified, removing the setValue of dvt_requested_for, and controling automaticly with the js code I put in the variable) :
function onCondition(onChange, control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Enable or disable the requested variable field based on the ITIL role
if (g_user.hasRoleExactly('itil')) {
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', false);
} else{
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', true);
}
}
//javascript:gs.getUserDisplayName(); this code in the dvt_requested_for default value

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 10:11 AM - edited 09-19-2023 10:12 AM
Try something like below
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Enable or disable the requested variable field based on the ITIL role
if (g_user.hasRoleExactly('itil')) {
g_form.setValue('dvt_requested_for', g_user.userID);
g_form.setReadOnly('dvt_requested_for', true);
} else {
g_form.setReadOnly('dvt_requested_for', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 12:54 AM
Do I still put a defaulyt value like this :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 01:15 AM
Thank you @Mike Patel you helped to find the correct solution.
So I added to my 'Requested for' variable a default value which is a js code like this -> javascript:gs.getUserDisplayName();
Then instead of a client script, I created a UI Policy by choosing Run script and I put the following code ( @Mike Patel modified, removing the setValue of dvt_requested_for, and controling automaticly with the js code I put in the variable) :
function onCondition(onChange, control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Enable or disable the requested variable field based on the ITIL role
if (g_user.hasRoleExactly('itil')) {
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', false);
} else{
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', true);
}
}
//javascript:gs.getUserDisplayName(); this code in the dvt_requested_for default value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 01:15 AM
The correct solution.
So I added to my 'Requested for' variable a default value which is a js code like this -> javascript:gs.getUserDisplayName();
Then instead of a client script, I created a UI Policy by choosing Run script and I put the following code ( @Mike Patel modified, removing the setValue of dvt_requested_for, and controling automaticly with the js code I put in the variable) :
function onCondition(onChange, control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Enable or disable the requested variable field based on the ITIL role
if (g_user.hasRoleExactly('itil')) {
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', false);
} else{
//g_form.setValue('dvt_requested_for', g_user.userName);
g_form.setReadOnly('dvt_requested_for', true);
}
}
//javascript:gs.getUserDisplayName(); this code in the dvt_requested_for default value