Requesting help with script include & client script for retrieving user's department

C_dric Chen
Tera Contributor

Greetings, everyone.

I'm attempting to retrieve a user's department from the sys_user table.I carefully went through the GlideAjax exercise, but I got stuck at the client script, which mentions a method called  getEmail, since I have no idea where this method was defined or how I can define a method to retireve a user's department instead.

Below is the Script include that I managed to complete:

var GetUserDepartment = Class.create();

  // Extend the global.AbstractAjaxProcessor class
  GetEmailAddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{

    getDepartment: function() {  // Define the getDepartment function.  
    var userRecord = new GlideRecord("sys_user");  // Create a GlideRecord for the User table.
    userRecord.get(this.getParameter('sysparm_userID'));  // Use the sysparm_userID passed from the client side to retrieve a record from the User table.
    return userRecord.department + '';  // Return the department for the requested record
  },

  type: 'GetUserDepartment'
});

(BTW, does anyone know how I can check the computer-friendly names of the columns in the sys_user table?)

Would anyone be kind enough to give me hints on how I can complete the client script?

Any help would be greatly appreciated!

 

13 REPLIES 13

Anil Lande
Kilo Patron

Hi,

This script looks good.

Can you please share your client script part?

Want to see how you are calling it.

 

Make sure to use script include API Name if you are calling it in scoped application.

Your code is client script should be like below:

var gaAjax = new GlideAjax('scope_name.GetUserDepartment');  // use script include api name here
gaAjax.addParam('sysparm_name','getDepartment');
gaAjax.addParam('sysparm_userID', newValue);  // pass the user sys_id here
gaAjax.getXML(callback);  // or use getXMLAnswer or getXMLWait

 

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Mahesh23
Mega Sage

Hi,

Try below code in your client script 

var ga = new GlideAjax('scope_name.GetUserDepartment');  // use script include name here
ga.addParam('sysparm_name','getDepartment');
ga.addParam('sysparm_userID', newValue);  // if you're using onChange on user field/variable then pass newValue here if you're using any other client script then pass sys_id of the user
ga.getXMLAnswer(callback);  
function callback(response) {
var answer = response; // answer holds the response of script include 
g_form.setValue("FIELD_NAME",answer)
}

 

Please mark my response as correct answer or helpful if applicable 

C_dric Chen
Tera Contributor

Hello.

So, below is the client script that I wrote, based on the responses I got above:

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

   //Type appropriate comment here, and begin script below
	var gaAjax = new GlideAjax('x_123456_<app_name>_0.GetUserDepartment');
	gaAjax.addParam('sysparm_name', 'getDeaprtment');
	// IF using onChange on user field/variable 
	// THEN pass newValue here 
	// IF you're using any other client script 
	// THEN pass sys_id of the user
	gaAjax.addParam('sysparm_userID', newValue);
	
	gaAjax.getXML(callback);
	function callback(response) {
		// answer_Dept holds the response of script include 
		var answer_Dept = response;
		g_form.setValue('department', answer_Dept);
	}
   
}

And here's the response I got:

find_real_file.png

What should I do?

Refer article for a check.