User role script item on a variable

Jeff W NZAO B
Mega Guru

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

JeffWNZAOB_0-1695136234309.png 

itil

JeffWNZAOB_1-1695136371525.png

 

Thanks for return

 

 

1 ACCEPTED SOLUTION

Jeff W NZAO B
Mega Guru

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

 

 

View solution in original post

6 REPLIES 6

Mike Patel
Tera Sage

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);
    }
}

 

Do I still put a defaulyt value like this : 

javascript:gs.getUserDisplayName(); 
 
JeffWNZAOB_0-1695196481623.png

 

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

 

 

Jeff W NZAO B
Mega Guru

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