Set requested_for in Variable set as read only when the user select one specific option.

rafaelalves4337
Tera Contributor

Hello team,

 

I have to set the requested for variable in variable set as read only and change it to the user that is logged in and doing the request.

 

I have created this script, but it's not working fine, I have 3 options, the first one the user can change the requested for, but the other 2 no, so the script bellow only works when I change from the first option to the second, but if i change from the third option to the second it allows the user to change the requested for again.

 

Am I doing something wrong?

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
	return;
}

var user=g_user.userID;

if(newValue == 'Remove access from a Group')
{
	g_form.setMandatory("requested_for",true);
	g_form.setValue("requested_for",user);
	g_form.setReadOnly("variables.requested_for", true);
	
}
}

 

 

1 ACCEPTED SOLUTION

SN_Learn
Kilo Patron
Kilo Patron

Hi @rafaelalves4337 ,

 

Please try the below, I have tested in PDI and it is working

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var user = g_user.userID;

//Don't allow changes(opt 2)
    if (newValue == 'Remove access from a Group') {
        g_form.setMandatory("requested_for", true);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", true);
    } 
	
	//Allow changes(Option 1)
	else if (newValue == '<update with opt 1>') {
        g_form.setMandatory("requested_for", false);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", false);
    } 
	//Don't allow changes(opt 3)
	else if (newValue == '<replace with opt3>') {
        g_form.setMandatory("requested_for", false);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", false);
    }
}

 

Please mark this response correct if I've answered your question.  Thanks!

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

If this is your entire script you are missing else if conditions for the other 2 options, or just an 'else' if they are both the same.  You shouldn't/don't need to specify 'variables.' in the setReadOnly. Outside of this script, do you have any other scripts or Catalog UI Policies that affect the requested_for variable?

SN_Learn
Kilo Patron
Kilo Patron

Hi @rafaelalves4337 ,

 

Please try the below, I have tested in PDI and it is working

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var user = g_user.userID;

//Don't allow changes(opt 2)
    if (newValue == 'Remove access from a Group') {
        g_form.setMandatory("requested_for", true);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", true);
    } 
	
	//Allow changes(Option 1)
	else if (newValue == '<update with opt 1>') {
        g_form.setMandatory("requested_for", false);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", false);
    } 
	//Don't allow changes(opt 3)
	else if (newValue == '<replace with opt3>') {
        g_form.setMandatory("requested_for", false);
        g_form.setValue("requested_for", user);
        g_form.setReadOnly("requested_for", false);
    }
}

 

Please mark this response correct if I've answered your question.  Thanks!

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

tdevine1
Tera Contributor

I think that you will have to create an else statement to reset the values.

 

if (newValue == 'Remove access from a Group') {
	g_form.setMandatory("requested_for",true);
	g_form.setValue("requested_for",user);
	g_form.setReadOnly("variables.requested_for", true);	
} else if (newValue== 'Other Value' || newValue == 'Other Value' {
	g_form.setMandatory("requested_for",true);
	g_form.setValue("requested_for",user);
	g_form.setReadOnly("variables.requested_for", true);
}

or switch over to a UI Policy 

Sorry, forgot to chage the values in the else to be false..