scriptinclude

bharath6009
Kilo Contributor

Q: Not getting the values from server side

 

client side scrip;

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert("CS RUN" + g_form.getValue('caller_id'));

var abc = g_form.getValue('caller_id');
var ga = new GlideAjax('global.userdetails');
ga.addParam('sysparm_name', 'callerdetails');
ga.addParam('sysparm_user', 'abc');
ga.getXML(pop);

function pop(response) {
var bp = (response.responseXML.documentElement.getAttribute("answer"));
alert(bp);
}
}

 

 

 

scripinclude code:

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

callerdetails: function() {
var cid = this.parameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', cid);
gr.query();
if (gr.next()) {
return gr.first_name;
}
},

type: 'userdetails'
});

 

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, what are the results of your debugging?
what parts of your code do\do not work as expected?
Is you script-include flagged as Client callable?

 

Looking at the code supplied, the obvious syntax error is that you are assigning a string 'ABC' to sysparm_user,
where as I think you need to map your variable ABC

ga.addParam('sysparm_user', abc);

 

Hi,

syntax error is that you are assigning a string 'ABC' to sysparm_user : i have defined the abc value i.e var abc = g_form.getValue('caller_id');

i want to get the result of first name of a caller ( return gr.first_name;) , but im getting the return value as "survey"

Bert_c1
Kilo Patron

Hi bharath6009,

 

Tony's comment is on the 'ga.addParam(('sysparm_user', 'abc');' in your script, not the value of abc. Anyway, I tried what you have using script include content:

 

 

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

	callerdetails: function() {
		var cid = this.parameter('sysparm_user');
		gs.info("userdetails: cid = " + cid);
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', cid);
		gr.query();
		gs.info("userdetails: Found " + gr.getRowCount() + " user records to process for sys_id: " + cid + ".");
		if (gr.next()) {
			return gr.first_name;
		}
		else {
			var ans = "Not Found";
			return ans;
		}
    },
	
    type: 'userdetails'
});

 

And log messages show cid is undefined. So some problem passing that to the script include.

 

You don't need a script include for what you say, the following onChange client script for incident.caller_id field works.

 

 

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

   //Type appropriate comment here, and begin script below
//	alert("CS RUN " + g_form.getValue('caller_id'));

	var abc = g_form.getValue('caller_id');
	var gr = new GlideRecord('sys_user');
	gr.addQuery('sys_id', abc);
	gr.query();
	if (gr.next()) {
		alert(gr.first_name);
	}
	else {
		alert("Not Found");
	}
	/*
	var gla = new GlideAjax('global.userdetails');
	gla.addParam('sysparm_name', 'callerdetails');
	gla.addParam('sysparm_user', abc);
	gla.getXML(pop);

	function pop(response) {
		var bp = (response.responseXML.documentElement.getAttribute("answer"));
		alert("response = " + bp);
	}
	*/
}

 

It would be interesting to learn why the parameter passing to the script include is not working.

I have fixed the problem. that is to follow API documentation in scripts.

 

As Tony found, you need to pass the value of 'abc', not a string: "abc" in the client script.  Also, in the script include use

 

 

var cid = this.getParameter('sysparm_user');

 

as:

 

var cid = this.parameter('sysparm_user');

 

is not correct. 

 

@bharath6009 please review this answer and respond here.