Cannot read properties of null error when trying to read in multiple functions from script include

gunishi
Tera Guru

 

Hi all, 

 

I have some code that follows the structure below (it is different but as I cannot copy and paste form my company laptop the code below will suffice. However, whenever I call it in a catalog client script it throws an error:

 

Error MessageonSubmit script error: TypeError: Cannot read properties of null (reading 'functionA'): function () { [native code] }

 

I emptied the whole of getfunctionA and replaced it with just a 'return false;' line, and still it is not reading it in correctly in my client script. 

 

Any help with this would be appreciated as the issue isn't with the code in getfunctionA, it is in the calling of the function in getAll. 

 

Thank you in advance, 

G

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

	getAll: function(){
		var param1 = this.getParameter("sysparm_param1");
		var param2 = this.getParameter("sysparm_param2");
		var param3 = this.getParameter("sysparm_param3");
		var param4 = this.getParameter("sysparm_param4");
		var param5 = this.getParameter("sysparm_param5");
		var param6 = this.getParameter("sysparm_param6");

		var data = {
			functionA: "",
			functionB: "",
			functionC: "",
		};
		data.functionA = getFunctionA(param1, param2);
		data.functionB = getFunctionA(param3, param4);
		data.functionC = getFunctionA(param3, param6);

		return JSON.stringify(data);
	},

	getFunctionA: function(param1, param2){
		return "Some data from A";
	},

	getFunctionB: function(param3, param4){
		return "Some data from B";
	},

	getFunctionC: function(param5, param6){
		return "Some data from C";
	},

    type: 'TestMultiFunctions'
});

 

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

Hi @gunishi 

 

The problem with your original code was that you were calling the getFunctionA(), getFunctionB(), and getFunctionC() functions without the this keyword.

This caused the functions to be called in the global scope, which is why you were getting the error message "TypeError: Cannot read properties of null (reading 'functionA'): function () { [native code] }".

 

Try calling the functions with the this keyword -

 

 

getAll: function(){
    // Get the parameters from the request.
    var param1 = this.getParameter("sysparm_param1");
    var param2 = this.getParameter("sysparm_param2");
    var param3 = this.getParameter("sysparm_param3");
    var param4 = this.getParameter("sysparm_param4");
    var param5 = this.getParameter("sysparm_param5");
    var param6 = this.getParameter("sysparm_param6");

    // Create an object to store the results of the functions.
    var data = {
        functionA: "",
        functionB: "",
        functionC: "",
    };

    // Call the functions using the "this" keyword.
    data.functionA = this.getFunctionA(param1, param2);
    data.functionB = this.getFunctionB(param3, param4);
    data.functionC = this.getFunctionC(param5, param6);

    // Return the results as a JSON string.
    return JSON.stringify(data);
},

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

View solution in original post

2 REPLIES 2

Tushar
Kilo Sage
Kilo Sage

Hi @gunishi 

 

The problem with your original code was that you were calling the getFunctionA(), getFunctionB(), and getFunctionC() functions without the this keyword.

This caused the functions to be called in the global scope, which is why you were getting the error message "TypeError: Cannot read properties of null (reading 'functionA'): function () { [native code] }".

 

Try calling the functions with the this keyword -

 

 

getAll: function(){
    // Get the parameters from the request.
    var param1 = this.getParameter("sysparm_param1");
    var param2 = this.getParameter("sysparm_param2");
    var param3 = this.getParameter("sysparm_param3");
    var param4 = this.getParameter("sysparm_param4");
    var param5 = this.getParameter("sysparm_param5");
    var param6 = this.getParameter("sysparm_param6");

    // Create an object to store the results of the functions.
    var data = {
        functionA: "",
        functionB: "",
        functionC: "",
    };

    // Call the functions using the "this" keyword.
    data.functionA = this.getFunctionA(param1, param2);
    data.functionB = this.getFunctionB(param3, param4);
    data.functionC = this.getFunctionC(param5, param6);

    // Return the results as a JSON string.
    return JSON.stringify(data);
},

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

Hi @Tushar  

 

Thank you so much, this has solved the issue.

 

G