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

Actually just worked it out! moved response to a g_form.setValue for the field!

Thanks so much for all your help! 😄

Happy to help, please close out this thread, by marking the correct answer.

Thanks

Hi Simon

I still seem to be getting null in the alert for this solution

Satyasarat Pato
Tera Guru

Hi Russel,

 

Check the below code for your scenario, Hope it helps.

Client Script:

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

    var userid = g_user.userID;
	var ga = new GlideAjax('getManager');
	ga.addParam('sysparm_name','getManager');	
	ga.addParam('sysparm_userid',userid);
	ga.getXML(autoFill);
   
}

function autoFill(response){
 	var manager = (response.responseXML.documentElement.getAttribute("answer"));
	
	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');
	g_form.setValue('assigned_to',manager);
	
}

 

Script Include:

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

	getManager: function(){
		
		var manager = '';
		var user_id = this.getParameter('sysparm_userid');
		var User = new GlideRecord('sys_user');
		User.addQuery('user_name', user_id);
		if(User.next()){
			manager = User.manager.getDisplayValue();
		}
		
		return manager;
	},
    type: 'getman'
});