removeOption on Status Field - I want to remove the ability to choose it, not the ability to see it

kemmy1
Tera Guru

I have a Status field that has 8 choices.  I only want a user with a particular role to be able to choose the "Cancelled" choice (only when the status is "Review", so I created an onload Client Script to check if the user has that role and removeOption on all the other choices except "Cancelled".

 

The issue is that when the user goes to open up a record that has a status other than "Cancelled" the status field is blank.

 

I also created an ACL that makes the field writeable only if it's in Review status.

 

I tried an onChange Client script and it shows the status if it's not cancelled, but then when I go to change the status, it allows that user to select any choice.

5 REPLIES 5

Akash4
Kilo Sage
Kilo Sage

Hello,

This gets complicated by involving ACLs now and later. Hiding rest of the choices but onLoad the existing state choices (other than Cancelled) are still shown. I tried in my PDI, its working as needed.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }
    var userCanBypass = g_user.hasRole('admin'); // role check
    if (userCanBypass) { //if user has role
        var stateField = g_form.getControl('state');
        if (stateField) {
            var options = stateField.options;
            for (var i = options.length - 1; i >= 0; i--) {
                if (options[i].value !== '8') { //assuming cancelled is 8
                    g_form.removeOption('state', options[i].value);
                }
            }
        }
        if (newValue != '8') {
            g_form.setValue('state', oldValue);
            g_form.showFieldMsg('state', 'You can only change the state to "Cancelled".', 'error');
        }
    }
}

 

 

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

Akash4
Kilo Sage
Kilo Sage

Add if (stateField && g_form.getValue('state','Review')){ in the line as shown below. Adjust as needed.

        if (stateField) {

 

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

This is so close, when it tries to go back to the oldValue, the onChange kicks off again and gives a double error message.  

kemmy1_0-1721945258876.png

 

You can add gs.hideFieldMsg() function just after the if block (newValue !=8)

So everytime the script runs next time, previous value is cleared.

Regards, Akash

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.