Getting error, "onChange script error: ReferenceError: answer is not defined function () { [native code] }"

Sumit14
Kilo Contributor

I want to set the email and manager of the caller. I have written script Include and then called that in my client script but it is giving me the error, "onChange script error: ReferenceError: answer is not defined function () { [native code] }" below the caller field but its working correctly. I wanted to know why it the error is coming? Thank you so much for the help!

Script Include:

var setEmailandManager = Class.create();
setEmailandManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	populateEmailAndManager: function()
	{
		var userID = this.getParameter('sysparm_user');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',userID);
		gr.query();
		if(gr.next())
			{
				var obj = {};
				obj.email = gr.email;
				obj.manager = gr.manager;
				return JSON.stringify(obj);
			}
	},

    type: 'setEmailandManager'
});

Client Script:

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

   //Type appropriate comment here, and begin script below
	var ga = new GlideAjax('setEmailandManager');
	ga.addParam('sysparm_name','populateEmailAndManager');
	ga.addParam('sysparm_user',newValue);

	var obj = JSON.parse(answer);
	g_form.setValue('email',obj.email);
	g_form.setValue('manager',obj.manager);

   
}
1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Thats because the answer variable is not defined. the following line is missing.

var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
	var obj = JSON.parse(answer);
	g_form.setValue('email',obj.email);
	g_form.setValue('manager',obj.manager);
    }

 

Mark the comment as a correct answer and helpful if this has answered the question.

View solution in original post

1 REPLY 1

asifnoor
Kilo Patron

Hi,

Thats because the answer variable is not defined. the following line is missing.

var response = ga.getXMLAnswer(parseResponse);
    function parseResponse(answer) {
	var obj = JSON.parse(answer);
	g_form.setValue('email',obj.email);
	g_form.setValue('manager',obj.manager);
    }

 

Mark the comment as a correct answer and helpful if this has answered the question.