I suggest you look at existing OOB client scripts and script includes. A simple example of an 'ohChange' client script getting a string value from a script include follows.

 

Client Script code:

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

	if (oldValue == newValue) {
		return;
	}

	//Type appropriate comment here, and begin script below
	var ga = new GlideAjax('XUserDetailsAjax');
	ga.addParam('sysparm_name','getCompany'); // function in the script include we're calling
	var usr = g_form.getValue('submitter');
	ga.addParam('sysparm_user_id', usr);

	/* Call function with user set and use the callback function getResponse() to return the result when ready */
	ga.getXMLAnswer(getResponse);  		
}

// callback function for returning the result from the script include
function getResponse(response) {
//	alert(response);
	g_form.setValue('u_company', response);
}

Script Include named 'xGetUserDetails' with Client Callable checked:

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

	getCompany: function() {
        var userID = this.getParameter("sysparm_user_id");
		var result = "";
 
		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', userID);
		user.setLimit(1);
		user.query();
		if (user.next()) {
			result = user.company.toString();
		}
		else {
			result = "Unknown";
		}
		return result;
    },

    type: 'XUserDetailsAjax'
});

Good luck.

View solution in original post