Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Ok, I have created the 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){
		return user.sys_id;// returns manager's sys_id
	}
	

},

    type: 'getman'
});

and added the the code to the client script to call the script include:

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('assigned_to',g_user.manager) ;
		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');
		var ga = new GlideAjax('GetManagerEmail') ;
ga.addParam('sysparm_name','getManager');
ga.getXMLWait();
alert(ga.getAnswer());
	}

but im just getting an alert box which says 'Nul' What am i doing wrong?

Dont use getXMLWait()

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

alert(response);



});

 

BTW.

In your script include, you need to return user.getValue('manager') - not user.sys_id

 

Now I just get this error:

find_real_file.png

replace

g_user.userID()) ;

with 

g_user.userID) ;

in ga.addParam('sysparm_id',

Odd, dont know how those got in there.

I have removed them, I dont get the error. But I dont get the message box either.

Not that I want a message box, I want one of the form fields to be filled in. I just want to make sure that its pulling back a value for now.