Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to use GlideAjax?

ayushaggarw
Giga 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?

5 REPLIES 5

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

Tanushree Maiti
Tera Sage

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:

nawalkishos
Tera Guru

Hi @ayushaggarw ,

Review the link- https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430

 

If you found my response helpful , please mark it as helpful 👍 and accept it as the solution .

Thank you! 🙏
Nawal Singh