On change client script error

Not applicable

Hi All,

 

We are getting an error on state field 'onChange script error: TypeError: Cannot read properties of null (reading 'up') function onChange_change_request_state_14(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } if (newValue != "-5" || newValue != "-4") { $("impact_annotation").up().hide(); } else { $("impact_annotation").up().show(); } }'.

 

Below is the on change client script

 

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


    if (newValue != "-5" || newValue != "-4") {
        $("impact_annotation").up().hide();
    } else {
        $("impact_annotation").up().show();
    }

}


function showImpactAnnotation(bs) {
    if (bs.next()) {
        // Show annotation if high business criticality.
        if ((bs.busines_criticality == '1 - most critical') || (bs.busines_criticality == '2 - somewhat critical')) {
            // Hide the annotation
            $('my_annotation').up().show();
        }
        else {
            // Hide the annotation
            $('my_annotation').up().hide();
        }
    }
    else {
        // Hide the annotation
        $('my_annotation').up().hide();
    }
}
Can anyone please help me on how to get rid of that error. Thanks in advance
5 REPLIES 5

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

If you are trying to hide and show field based on condition match then you can use g_form.setDisplay('impact_annotation', false); and g_form.setDisplay('impact_annotation', true); instead of  $("impact_annotation").up().hide();.

Error state that "up" is not readable.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
LinkedIn: https://www.linkedin.com/in/runjay
YouTube: https://www.youtube.com/@RunjayP

-------------------------------------------------------------------------

Pradeep Thipani
Mega Sage

Hi @Community Alums ,

 

Looks like there are slight changes required in your script, 

1)New value can't be both "-5" and "-4" simultaneously. We should use and instead.

2) Check weather $ function  is working as expected or not if not replace with document.getElementById.

 

 

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

    if (newValue != "-5" && newValue != "-4") {
        $("impact_annotation").up().hide();
    } else {
        $("impact_annotation").up().show();
    }
}

function showImpactAnnotation(bs) {
    if (bs.next()) {
        
        const isCritical = bs.busines_criticality == '1 - most critical' || 
                           bs.busines_criticality == '2 - somewhat critical';
        $("my_annotation").up().setVisible(isCritical);
    } else {
        $("my_annotation").up().hide();
    }
}

 

Thanks,

Pradeep

 

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

Also you can use like below.

 

var element = document.getElementById('impact_annotation');
        if (element) {
            element.parentElement.style.display = 'none'; // Hides the parent element
        }
 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
LinkedIn: https://www.linkedin.com/in/runjay
YouTube: https://www.youtube.com/@RunjayP

-------------------------------------------------------------------------

Not applicable

Hi Runjay, I have modified the code as below and annotation should display only in new and assess state but it is displaying in all states.

 

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


    if (newValue != "-5" || newValue != "-4") {

        var element = document.getElementById('impact_annotation');        
        if (element) {            
            element.parentElement.style.display = 'none';
            }
        //g_form.setDisplay('impact_annotation', false);
        //$("impact_annotation").up().hide();
    } else {
       
       // g_form.setDisplay('impact_annotation', true);
       // $("impact_annotation").up().show();
    }

}


function showImpactAnnotation(bs) {
    if (bs.next()) {
        // Show annotation if high business criticality.
        if ((bs.busines_criticality == '1 - most critical') || (bs.busines_criticality == '2 - somewhat critical')) {
            // Hide the annotation
           
            $('my_annotation').up().show();
        } else {
            // Hide the annotation
            $('my_annotation').up().hide();
        }
    } else {
        // Hide the annotation
        $('my_annotation').up().hide();
    }
}