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

Could you copy paste the Script Include and Client Script exactly as you have it?

Removing global is not a good idea.

To your original question on how to check field names, you can right-click on a field's label and select the Show - '...' context menu item:

find_real_file.png

Actually that already tells you the field name, but clicking on it reveals further details and enables you to copy the name:

find_real_file.png

When needing the same info for fields that are not on the form, one can bring up the xml view of records that contains the field name (and values):

find_real_file.png

Here's the XML view:

find_real_file.png

Also, if installing browser extensions is an option, many prefer using @Arnoud Kooi's SNUtils - just by double clicking on a form, not only reveals the field names, but the choice values to:

find_real_file.png

Hi,

The message suggests problem with scopes.

Is the scope of Client Script and Script Include the same?

I've re-created the Script Include as a scoped application.

Script Include. The content of the script is the same but I've renamed both the Client Script and Script Include to avoid conflict with those I've created under Global.

find_real_file.png

I've also re-created a Maintain Item as a scoped application in the same scope.

find_real_file.png

 

If the Script Include is created in Global scope, change as follows to use it from Client Script.

Script Include. Set "Accessible from" to "All application scopes".

find_real_file.png

Client Script in scoped

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //var ajax = new GlideAjax('GetUserDepartmentScoped');
	var ajax = new GlideAjax('global.GetUserDepartment'); // prefix Script Include name with "global."
    ajax.addParam('sysparm_name', 'getDepartment');
    ajax.addParam('sysparm_userID', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('department', answer);
        }
    });
}

Konichiwa.

So I tried everything to make sure both the script include and the client script are in the global scope.

Script include:

find_real_file.png

var GetUserDepartment= Class.create();

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

Client script:

find_real_file.png

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

   //Type appropriate comment here, and begin script below
	var gaAjax = new GlideAjax('global.x_<API_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.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('department', answer);
        }
    });
   
}

Then I got this error message:

find_real_file.png

"Access to Script include blocked from scope."

I then tried to return to the landing page and set my scope to "Global" from the ???? menu (top right), but the "Blocked from scope" error message keeps coming back.

What should I do next?