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

Hi,

You are using getXML(), in this case we need to use getXMLAnswer().

Please check below article to learn more:

https://community.servicenow.com/community?id=community_article&sys_id=1c10a1fedbbd4890feb1a851ca961...

 

You can update your client script like below:

// THEN pass sys_id of the user
	gaAjax.addParam('sysparm_userID', newValue);
	
	gaAjax.getXMLAnswer(callback);  // one small change here
	function callback(response) {
		// answer_Dept holds the response of script include 
		var answer_Dept = response;
		g_form.setValue('department', answer_Dept);
	}

Or use below logic with response.responseXML.documentElement.getAttribute("answer"):

 

// 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.responseXML.documentElement.getAttribute("answer");  / or change this line
		g_form.setValue('department', answer_Dept);
	}

 

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

Hi,

Did you get chance to look at the solution proposed by me?

 

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

Checking right now.

Apologies for the lack of response. I'm the only developer, and I have to go back and forth between multiple user stories. =P

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Cédric,

First, is the field Department a reference field to cmn_department table or a Single Line Text? If the original example was using email, it's probably a Single Line Text. However, department in user table is a reference field so returning value of department will only show sys_id of user's department.

Case 1: Department is a reference field to table cmn_department.

Client script

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
    var ajax = new GlideAjax('GetUserDepartment');
    ajax.addParam('sysparm_name', 'getDepartment');
    ajax.addParam('sysparm_userID', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('department', answer);
        }
    });
  }

Script include. I've changed "GetEmailAddress.prototype " to "GetUserDepartment.prototype"

var GetUserDepartment= Class.create();
GetUserDepartment.prototype = Object.extendsObject(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'
});

Execution result

find_real_file.png

Case 2: Department is a Single Line Text

Client script is the same as above.

Script include

var GetUserDepartment= Class.create();
GetUserDepartment.prototype = Object.extendsObject(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.name; // Return the department for the requested record
    },
    type: 'GetUserDepartment'
});

Execution result

find_real_file.png

Konichiwa!

I tried your script include, and I got this error message:

find_real_file.png

But when I replaced "Object.extendsObject(AbstractAjaxProcessor, {" with "Object.extendsObject(global.AbstractAjaxProcessor, {", the script include simply stopped working.

What should I do? (╥︣﹏᷅╥)