Query in client script?

russellprice
Tera Contributor

Hi

I have an onchange client script which auto fills fields depending on the person who is logged in and what catergerys are seleceted:

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

	if (g_form.getValue('u_subcategory') == 'New' && g_form.getValue('u_category') == 'Certificate') {
		g_form.setValue('u_requested_by',g_user.userID) ;
		g_form.setValue('short_description','New Certificate Request');
		g_form.setValue('description','Certificate name:\r\nCertificate Type (SSL, SSL with EV, Wildcard, Code signing):\r\nCertificate Term:\r\nServer Software:\r\nRegion if applicable (EMEA, ASPAC,US etc)\n\n');
	}

I have got it to fill in a field depending on the user (g_user.userID) but I would also like it to fill in the users manager. I dont beleive I can use g_user for this and have to query the sys_user table? I am new to scripting and im not sure how I would encorperate a query into this script.

Any suggestions would be much appreciated.

23 REPLIES 23

can you post your current script?

Thanks

Client Script:

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

	
	if (g_form.getValue('u_subcategory') == 'New' && g_form.getValue('u_category') == 'Certificate') {
			var ga = new GlideAjax('getManager') ;
ga.addParam('sysparm_name','getManager');
ga.addParam('sysparm_id', g_user.userID);
ga.getXMLAnswer(function(response){
alert(response);
});
	
		g_form.setValue('u_requested_by',g_user.userID) ;
		g_form.setValue('short_description','New Certificate Request');
		g_form.setValue('description','Certificate name:\r\nCertificate Type (SSL, SSL with EV, Wildcard, Code signing):\r\nCertificate Term:\r\nServer Software:\r\nRegion if applicable (EMEA, ASPAC,US etc)\n\n');

	}

Script include:

var getman= Class.create();
getman.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
getManager: function(){
	
	var id = this.getParameter('sysparm_id'); //get user_id passed from client script
var user = new GlideRecord('sys_user');
    user.get('user_name',id);
	if (user){
		user.getValue('manager');// returns manager's sys_id
		
	}
	

},

    type: 'getman'
});

the results of the script include should go into the 'assigned_to' field

your script include name is incorrect. "getman" is the name of the script include right?

 

var ga = new GlideAjax('getman') ;
ga.addParam('sysparm_name','getManager');

Of course! Its now bringing up the message box but its still 'null' So I assume its not pulling back the manager name? Is user.getValue('manager'); Correct for the script include?

Sorry

Remember that your response is in a function and when you get your reponse you can then do specific logic when used inside the function

In your script include remember to "return user.getValue('manager');"

Not just user.getValue('manager');

 

if (g_form.getValue('u_subcategory') == 'New' && g_form.getValue('u_category') == 'Certificate') {

	var ga = new GlideAjax('getman') ;
        ga.addParam('sysparm_name','getManager');
        ga.addParam('sysparm_id', g_user.userID);
        ga.getXMLAnswer(function(response){
        alert(response);

        g_form.setValue('u_requested_by',g_user.userID);
        g_form.setValue('short_description','New Certificate Request');
	g_form.setValue('description','Certificate name:\r\nCertificate Type (SSL, SSL with EV, Wildcard, Code signing):\r\nCertificate Term:\r\nServer Software:\r\nRegion if applicable (EMEA, ASPAC,US etc)\n\n');

});
	
}