How to use GlideAjax?

ayushaggarw
Tera Contributor

I have a task - 
When a user selects a Caller on an Incident form, the system should fetch the caller's department from the server
and populate a custom field on the form without reloading the page. Design the solution using a client callable
Script Include and a client script.


Conditions / expectations:
• Do not hardcode department values in the client script.
• Use GlideAjax for communication.
• Handle the case where the user has no department.
Success criteria: A working client-side interaction where the department is fetched from the server and populated
correctly.

I created column in incident table and create client script and script include and select the user with department but it does not autofill in the incident table why anyone help me out of this?

1 ACCEPTED SOLUTION

Its_Sagnic
Giga Guru

Hi @ayushaggarw ,

Hope you are doing well.

Try to use the below code snippets.

1. The Script Include : 

  • Step 1: You must check the "Client callable" box.
  • Step 2: 
var UserDetailsUtils = Class.create();
UserDetailsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { // Connects to the Ajax system

    getDept: function() {
        // 1. Get the User ID 
        var userId = this.getParameter('sysparm_user_id');
        
        // 2. Look up the User record in the database
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            // 3. Get the "Department" name
            var department = userGR.getDisplayValue('department');
            
            // 4. Return it, or a message if empty 
            return department ? department : "No Department Assigned";
        }
        return "";
    },

    type: 'UserDetailsUtils'
});
  •  Step 3The Client Script
         Type : onChange
         Field name: Caller
  • Step 4 :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        g_form.setValue('u_caller_department', ''); // Clear your custom field
        return;
    }

    // 1. Start the "GlideAjax" request
    var ga = new GlideAjax('UserDetailsUtils'); // Name of the Script Include
    ga.addParam('sysparm_name', 'getDept');      // Name of the function to run
    ga.addParam('sysparm_user_id', newValue);    // Pass the new Caller's sys_id

    // 2. Send the request and wait for the "answer" (getXMLAnswer is the modern way)
    ga.getXMLAnswer(function(answer) {
        if (answer) {
            // 3. Populate your custom field with the result 
            g_form.setValue('u_caller_department', answer);
        }
    });
}


Use the above code to get your solution. If you find it helpful for you please mark it as helpful.

Regards,

Sagnic

View solution in original post

7 REPLIES 7

Aditya_hublikar
Mega Sage

Hello @ayushaggarw ,

 

You can below code :

 

client script :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var ga=new GlideAjax('getDepartment');
   ga.addParam('sysparm_name','getUserData');
   ga.addParam('sysparm_user',newValue);
   ga.getXMLAnswer(function(res){
	g_form.setValue('short_description',res)
// here i set department value in short description , you can set in your custom field
   })

   //Type appropriate comment here, and begin script below
   
}

Screenshot (1190).png

 

Script include :

var getDepartment = Class.create();
getDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserData: function() {
        var gr = new GlideRecord('sys_user');
		gs.log(this.getParameter('sysparm_user'))
        if (gr.get(this.getParameter('sysparm_user'))) {
            if (gr.department) {
                return gr.getDisplayValue("department").toString();
				
            }
			else{
				gs.addErrorMessage('No valid department exist')
			}

        }
    },
    type: 'getDepartment'
});

 

 Screenshot (1189).png

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya

Hello @ayushaggarw ,

 

 I hope you are doing well . Does your query is resolved ?

 

If my response helps you then mark it as helpful and accept as solution .

Regards,

Aditya,

Tanushree Maiti
Kilo Patron

Hi @ayushaggarw 

 

Client Script

Table: Incident
Type: onChange
Field name: Caller
Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

	if(isLoading) {
		return;
	}

	if(newValue === '') {
		g_form.clearValue('u_custom_department');
	}

	var gaPhone = new GlideAjax('getUserPropertiesAjax');
	gaPhone.addParam('sysparm_name', 'get_department');
	gaPhone.addParam('sysparm_user', newValue);
	gaPhone.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		var answer = response;
		
		g_form.setValue('u_custom_department', answer);
	}

}

 

Script Include

Name: getUserPropertiesAjax
Client callable: true
Script:

var getUserPropertiesAjax = Class.create();
getUserPropertiesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	get_department : function() {
		var grUser = new GlideRecord('sys_user');

		if(grUser.get(this.getParameter('sysparm_user'))) {
			return grUser.getValue('department');
		}
	},
	
    type: 'getUserPropertiesAjax'
	
});
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: