Client Script to Set Field to Value + Read-Only Not Working As Expected

Maurice Murphy
Tera Guru

I'm trying to create an onChange client script and/or UI Policy for a field on a form which asks the user to confirm if their manager is correct. The manager field auto-populates from the Requested For reference field, and then the user must select a Yes or No if their manager is correct.

 

If yes, the approval is sent to that manager, if no, it goes through our manual approval process. 

 

The problem I'm facing is that with the client script, no matter if the manager field is blank or not, it will set the Yes/No field to No and Read-Only every time, when it should only be when it's blank. The UI Policy on the other hand is not working at all.

 

I don't have both active at once and have been testing them separately. Any thoughts on what could be going on?

 

Here's the client script version:

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

    // If there is no manager in the manager auto-fill, the field asking if the manager is correct
    // should automatically say 'No' and become read-only.
    // Otherwise, the field should be left alone.

    if (g_form.getValue(approving_manager_autofill) == '') {
        g_form.setValue('is_the_list_approving_manager_correct', 'No');
        g_form.setReadOnly('is_the_list_approving_manager_correct', true);
    } else {
        g_form.setReadOnly('is_the_list_approving_manager_correct', false);
    }
}


And here's the UI Policy:

MauriceMurphy_0-1705006562873.png

MauriceMurphy_2-1705006589952.png

function onCondition() {
g_form.setValue('is_the_list_approving_manager_correct', 'No');
}

 

 

2 REPLIES 2

Jim Bland
Tera Contributor

It looks like your getValue function is missing Quotations for the field name, so the 

 

if (g_form.getValue(approving_manager_autofill) == '') {

 

 

should be with " or ', I usually use ' as well

 

if (g_form.getValue("approving_manager_autofill") == '') {

 

 

Also if this is an on change of the manager field you don't need the getValue and just replace it with the newValue variable, this would contain the value of the field that changed.

 

    if (newValue == '') {
        g_form.setValue('is_the_list_approving_manager_correct', 'No');
        g_form.setReadOnly('is_the_list_approving_manager_correct', true);
    } else {
        g_form.setReadOnly('is_the_list_approving_manager_correct', false);
    }

 


Hopefully that is all that is keeping it from working.

Thanks for the help! The manager auto fills from the Requested For so I had tried the onChange for both of those fields and they weren't giving me success.

If I try with newValue with the Manager field, it doesn't change anything at all. When I try it with getValue on the Manager field and onChange on the Requested For field, it will always set the read only no matter what, even if the Requested For doesn't have a blank manager.

 

In addition, it seems like the Else to set the field back to editable doesn't work in any situation.