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

Robert H
Mega Sage

Hello @coreyhirsch ,

The problem is that your script is using the g_form.getEditableFields() function to get the fields to loop through. When the script runs and makes them read-only, the next time it runs g_form.getEditableFields() will not return much, because you just made the fields non-editable 🙂

Have you considered alternative approaches, like ACLs?

 

Edit:

If you need a client side solution, you can create an additional onLoad script with this line:

function onLoad() {
   g_scratchpad.editableFields = g_form.getEditableFields();
}

Then in your existing onChange script, change line #10 to:

var allFields = g_scratchpad.editableFields;

 

Regards,

Robert

coreyhirsch
Tera Contributor

@Robert H so it actually does work but only when I save the page, not immediately after I change the state

@coreyhirsch Please see the update I made to my original reply.

Hello @coreyhirsch 

 

It's because you have written this - newValue = g_form.getValue('state');

 

Dont write this, there's a purpose newValur was made in onChange , because it's able to capture the changes value on the form, even when its not saved, and that's the reason the change will immediately reflect. But when you change the newValue like this you are practically making it same as onLoad, which will take up newValue only when saved once. 

 

Remove that and let the form work by taking the newValue as it usually does. Dont change that part. @coreyhirsch 

 

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

 

Regards,

 

Shivalika 

 

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

 

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