On change client script error

Community Alums
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

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

Part 2. In this video i have talked about overview on ServiceNow platform/tool. How you can opt for personal dev instance (PDI)? how to login in ServiceNow instance and navigation to OOB modules. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on

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

 

"If this response was useful, please select 'Accept as Solution' and mark it as 'Helpful.' This helps me provide better answers and assists the community ".

Regards,
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

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

Part 2. In this video i have talked about overview on ServiceNow platform/tool. How you can opt for personal dev instance (PDI)? how to login in ServiceNow instance and navigation to OOB modules. For document please visit: https://servicenowwithrunjay.com/ Follow Facebook page for latest update on

Community Alums
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();
    }
}