iCannot read properties of null (reading 'toLowerCase')

Dave_p
Giga Guru

Hi,

I am getting an error 'Cannot read properties of null (reading 'toLowerCase') on GlideAJAX. Kindly help.

 

GlideAJAX

 

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

	var ci_name = newValue;
	
	var ga = new GlideAjax('get_app_code');
	ga.addParam('sysparm_name','getApplication');
	ga.addParam('sysparm_ci',ci_name);
	////
	ga.getXML(getInfo);

	}


	function getInfo(response)
	{   
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('ci_code1',answer.toLowerCase());
		//alert(answer);
   //Type appropriate comment here, and begin script below
   
}

 

1.png

 

Script Include

 

var get_app_code = Class.create();
get_app_code.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	
	
	getApplication: function()
{
	var ci = this.getParameter('sysparm_ci');
	
	var gr = new GlideRecord('cmdb_ci_service_auto');
	gr.addQuery('sys_id',ci);
	gr.query();
	if(gr.next())
		{
			return gr.u_application_code;
		}
	
	//gs.addErrorMessage(ci);
	
	//return ci;
	
},
    type: 'get_app_code'
});

 

2.png

 

3.png

 

Error: Unhandled exception in GlideAjax. Cannot read properties of null (reading 'toLowerCase')

 

Regards

Suman P.

4 REPLIES 4

Brian Lancaster
Tera Sage

Can you add an alert(answer) before the g_form.setValue to see what the script include is returning.

 

Edit: You can also try g_form.setValue('ci_code1',answer.toString().toLowerCase());

Juhi Poddar
Kilo Patron

Hello @Dave_p 

The toLowerCase() method works only on string values.
Make sure the answer variable is of type string before calling toLowerCase() on it.

You can verify the type by adding:

alert(typeof answer);

This will help ensure you're not calling the method on an undefined or non-string value.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

Chaitanya ILCR
Mega Patron

Hi @Dave_p ,

Update your script include like this (make sure it returns something

var get_app_code = Class.create();
get_app_code.prototype = Object.extendsObject(AbstractAjaxProcessor, {



    getApplication: function() {
        var appCode = '';
        var gr = new GlideRecord('cmdb_ci_service_auto');
        if (gr.get(this.getParameter('sysparm_ci'))) {
            appCode = gr.getValue('u_application_code');
        }
        return appCode ? appCode : '';
    },
    type: 'get_app_code'
});

 

client script

 

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

    var ga = new GlideAjax('get_app_code');
    ga.addParam('sysparm_name', 'getApplication');
    ga.addParam('sysparm_ci', newValue);
    ga.getXMLAnswer(getInfo);

}


function getInfo(answer) {
    g_form.setValue('ci_code1', answer ? answer.toLowerCase() : '');
    //alert(answer);
    //Type appropriate comment here, and begin script below
}

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya


 

Hristo Ivanov
Kilo Sage

Most likely your client-side getInfo() function is trying to call .toLowerCase() on a null value, probably the Script Include (get_app_code) is not returning any value when the GlideRecord query doesn’t find a matching record — i.e., answer becomes null.

You can add an alert as Brian suggested to confirm or add a null check before calling toLowerCase()

function getInfo(response) {   
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
        g_form.setValue('ci_code1', answer.toLowerCase());
    } else {
        g_form.setValue('ci_code1', ''); // or show a message, or leave as-is
    }
}