Unhandled exception in GlideAjax. Cannot read properties of null (reading 'length') in stage Env1

Kiran Maruthi
Tera Contributor

There is on change Catalog client script which will trigger when the request type field is changed. This client script will internally call script include.

While performing on change action ESC form I'm getting console error called (Only in STAGE environment)"Unhandled exception in GlideAjax. Cannot read properties of null (reading 'length')".

below this the client script:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) {
   
      return;
   }
   g_form.clearOptions("sub_request_e_c");
   

    var gaServiceGrpsAjax = new GlideAjax("SI Name");
    gaServiceGrpsAjax.addParam('sysparm_name''getSubRequest');
    gaServiceGrpsAjax.addParam('sysparm_requestType', newValue);
    gaServiceGrpsAjax.getXMLAnswer(function(answer) {
        var response = JSON.parse(answer);

        g_form.addOption('sub_request_e_c''''-- None --');
        for (var i = 0; i < response.length; i++) {
            g_form.addOption('sub_request_e_c', response[i], response[i]);
        }
        g_form.setValue('sub_request_e_c','');
    });

   //Type appropriate comment here, and begin script below
   
}
 
and here is the function in script include:
getSubRequest: function() {

        var requestType = this.getParameter("sysparm_requestType");
        var subRequests_list = [];
        var grSubRequest = new GlideRecord("table name");
        grSubRequest.addEncodedQuery('GOTOu_variable=sub_request_e_c^u_dependent_variable=request_type_e_c^u_active=true');
        grSubRequest.addQuery("u_dependent_variable_value", requestType);
        grSubRequest.orderBy("u_order");
        grSubRequest.query();
        while (grSubRequest.next()) {
            subRequests_list.push(grSubRequest.u_value.toString());
        }
        return JSON.stringify(subRequests_list);

    },
 
 
 
Pls note: this functionality is working as expected in DEV and TEST, but throwing the console error in STAGE after migration to stage.
 
pls suggest some solution to come out of this situation
 
2000102143_0-1718701757456.png

 

2 REPLIES 2

Abhijit4
Mega Sage

You may not have required data in Stage env. Basically your "subRequests_list" object in script include is empty which throws this error when you try to find its length in client script.

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Sandeep Rajput
Tera Patron
Tera Patron

@Kiran Maruthi Please update the onChange Client script as follows and see if the error disappears.

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) {
   
      return;
   }
   g_form.clearOptions("sub_request_e_c");
   

    var gaServiceGrpsAjax = new GlideAjax("SI Name");
    gaServiceGrpsAjax.addParam('sysparm_name', 'getSubRequest');
    gaServiceGrpsAjax.addParam('sysparm_requestType', newValue);
    gaServiceGrpsAjax.getXMLAnswer(function(answer) {
        var response = JSON.parse(answer);
       if(response){
        g_form.addOption('sub_request_e_c', '', '-- None --');
        for (var i = 0; i < response.length; i++) {
            g_form.addOption('sub_request_e_c', response[i], response[i]);
        }
}
        g_form.setValue('sub_request_e_c','');
    });

   //Type appropriate comment here, and begin script below
   
}