The CreatorCon Call for Content is officially open! Get started here.

onChnage script does not work

Julius28
Tera Expert

Hello,

 

Have created onChnage script for portal form, which should auto check checkbox called cbx_yes, if in previous questions user picks either Minor, Major or Critical and if environment is Production. Also goal would be to make this cbx_yes Read only when its auto checked. In browser console dont get any errors related to this script. Anyone seeing mistake? kinda stuck right now.

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

    (function() {
        var severityField = g_form.getControl('severity');
        var environmentField = g_form.getControl('environment');
        var cbxYesField = g_form.getControl('cbx_yes');


        severityField.observe('change', function() {
            updateCbxYesField();
        });

        environmentField.observe('change', function() {
            updateCbxYesField();
        });

        // Function to update cbx_yes field based on conditions
        function updateCbxYesField() {
            var severity = g_form.getValue('severity');
            var environment = g_form.getValue('environment');

            // Check conditions: Severity is Minor, Major, or Critical AND environment is Production
            if ((severity == 'Minor' || severity == 'Major' || severity == 'Critical') && environment == 'Production') {
                // Set cbx_yes field to true and make it read-only
                g_form.setValue('cbx_yes', true);
                g_form.setReadOnly('cbx_yes', true);
            } else {
                // Clear cbx_yes field and make it editable
                g_form.setValue('cbx_yes', false);
                g_form.setReadOnly('cbx_yes', false);
            }
        }

        // Initial update of cbx_yes field
        updateCbxYesField();
    })();
}

 

 Thanks again.

 

1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

According to the documentation, getControl() is not supported on the service portal. 

I'd highly recommend you do this using a UI policy, they're purpose is to do this sort of thing (ie: to control readonly/mandatory state of a fields). In the UI policy, you can set the condition to be if

- "severity" is one of "Minor", "Major" or "Critical"

and

- environment is "Production"

Then ensure that the UI policy has "Reverse is false" set and that "Applies on a Catalog Item view" is checked so that it runs on the service portal.

 

Then in the UI policy action you can set your cbx_yes field to be readonly. On the UI policy, in the Script tab, you can set the checkbox to true using the Execute if true script, and set it to false in the Execute if false script.  Just make sure that Run scripts in UI type is set to Both or Mobile / Service Portal to run on the portal view.

View solution in original post

2 REPLIES 2

Nick Parsons
Mega Sage

According to the documentation, getControl() is not supported on the service portal. 

I'd highly recommend you do this using a UI policy, they're purpose is to do this sort of thing (ie: to control readonly/mandatory state of a fields). In the UI policy, you can set the condition to be if

- "severity" is one of "Minor", "Major" or "Critical"

and

- environment is "Production"

Then ensure that the UI policy has "Reverse is false" set and that "Applies on a Catalog Item view" is checked so that it runs on the service portal.

 

Then in the UI policy action you can set your cbx_yes field to be readonly. On the UI policy, in the Script tab, you can set the checkbox to true using the Execute if true script, and set it to false in the Execute if false script.  Just make sure that Run scripts in UI type is set to Both or Mobile / Service Portal to run on the portal view.

Ivan Betev
Mega Sage
Mega Sage

Hi @Julius28 ,

 

additionally to what @Nick Parsons says, the observer method is not part of ECMA5 as far as I remember, unless you are in a custom app scope with ECMA2021 enabled.

 

Regards, Ivan