Client Script

coreyhirsch
Tera Contributor

Hello,

 

I am having trouble figuring out why my script is not working. The purpose of this script is to make sure all fields on the form are read-only when the state is cancelled. The only field that should not be read only is the State field in case users decide to change it from cancelled to something else. This part works fine.

 

The issue I am having is when I change the state from cancelled to something else, all fields should revert back to editable and mandatory (same as they were before the initial cancelled status). When I change the State field to something other than Cancelled, the fields remain read-only. Here is my script:


function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        newValue = g_form.getValue('state');
    }

    if (newValue === '') {
        return;
    }

    var allFields = g_form.getEditableFields();

    if (newValue == '7' || newValue == '5' || newValue == '4') {
        for (var i = 0; i < allFields.length; i++) {
            var fieldName = allFields[i];
            if (fieldName !== 'work_notes' && fieldName !== 'comments' && fieldName !== 'state') {
                g_form.setMandatory(fieldName, false);
                g_form.setReadOnly(fieldName, true);
            }
        }
    } else {
        for (var i = 0; i < allFields.length; i++) {
            var fieldName = allFields[i];
            g_form.setReadOnly(fieldName, false);
            g_form.setMandatory(fieldName, true);
        }
        g_form.clearMessages();
    }
}

 

 

5 REPLIES 5

Shivalika
Mega Sage

Hello @coreyhirsch 

 

Please confirm if you checked my answer. Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for my efforts and also it can move from unsolved bucket to solved bucket. 

 

Regards, 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeE