How to Auto populate the values by using Glide Ajax

don123
Kilo Contributor

Hi All,

How to auto populate the values in Reference fields (Name, Location) by using the Glide Ajax.

 

Regards

Adi Narayana

4 REPLIES 4

Harsh Vardhan
Giga Patron

here you will find your answer.

adding one thread here. kindly check that and use that script.

 

Auto populate user information

Harsh Vardhan
Giga Patron

Script Details:

 

Script Include:

Make the modification based on your need. 

var userdetails = Class.create();
userdetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	requestor_info: function() {
		
		var arr=[];
		
		var json = new JSON();
		var details=this.getParameter('sysparm_user_name');
		var user= new GlideRecord('sys_user');
		user.addQuery('sys_id',details);
		user.query();
		while(user.next())
			{
			
			var obj = {};
				obj.phone=user.phone.getDisplayValue();
				obj.location=user.location.getDisplayValue();
				obj.email=user.email.getDisplayValue();
				obj.manager=user.manager.toString();
				obj.country_code=user.country.toString();				
				obj.name=user.name.getDisplayValue();
				arr.push(obj);
			}
			gs.log('value is :'+ json.encode(arr));
			return (json.encode(arr));
			
		},
		type: 'userdetails'
	});

 

Client Script:

 

below client script has been written on incident form caller field which is onchange(), same way you can do that based on your requirement. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	var user = g_form.getValue('caller_id');
	var ga = new GlideAjax('userdetails');
	ga.addParam('sysparm_name','requestor_info');
	ga.addParam('sysparm_user_name',user);
	ga.getXML(HelloWorldParse);
	
	function HelloWorldParse(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		var ans = answer.evalJSON();
		for (var k = 0; k < ans.length; k++) {
			alert(ans[k].email);
			//g_form.setValue('phone',ans[k].phone);
			//g_form.setValue('location',ans[k].location);
			//g_form.setValue('email',ans[k].email);
					
			
		}
	}
	
}

 

 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

We are using the technique below for onChange auto populating values. Is designed to auto populated 1 field per onChange, though could easily be changed.
This also applies many good practices like not returning the whole xml answer, preventing an extra call to the database for reference fields (needs the displayValue + the value!!!), etc..

Works for both Client Scripts as Catalog Client Scripts.

Client callable Script Include:

// Class and function(s)
var QT_CoreConfigurationUtilsAjax = Class.create();
QT_CoreConfigurationUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	get_value: function() {
		
		// Get GlideAjax parameters
		var tableStr = this.getParameter('sysparm_table');
		var fieldStr = this.getParameter('sysparm_field');
		var sysId = this.getParameter('sysparm_sysid');
		var typeStr = this.getParameter('sysparm_type');
		
		// Get record
		var getTable = new GlideRecord(tableStr);
		
		if(getTable.get(sysId)) {
			var answer = {};

			if(typeStr == 'string') {
				answer.value = getTable.getValue(fieldStr);
			}
			if(typeStr == 'reference') {
				answer.value = getTable.getValue(fieldStr);
				answer.displayValue = getTable.getDisplayValue(fieldStr);
			}
			if(typeStr == 'displayValue') {
				answer.value = getTable.getDisplayValue(fieldStr);
			}
			
			// Return answer
			return JSON.stringify(answer);
		}
		
	},
		
	type: "QT_CoreConfigurationUtilsAjax"
	
});

onChange Client Script:

// Set the value of field_name based on the onChange newValue with the usage of Script Include 'QT_CoreConfigurationUtilsAjax'.
// Variables to update: lines 4, 5 and lines 7, 8.

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

	// Define variables -> Get
	var getTableStr = 'TABLE_NAME'; // Table
	var getFieldStr = 'FIELD_NAME'; // Field
	// Define variables -> Set
	var setFieldStr = 'FIELD_NAME'; // Field
	var setTypeStr = 'string'; // Type, 'reference' .OR. 'string' .OR. 'displayValue'
	
	// Default client script pre-checks
	if(!newValue) {
		return;
	}
	
	if(!isLoading && newValue == oldValue) {
		return;
	}
	
	// Script Include
	var gaCoreConfiguration = new GlideAjax('QT_CoreConfigurationUtilsAjax');
	gaCoreConfiguration.addParam('sysparm_name', 'get_value');
	gaCoreConfiguration.addParam('sysparm_table', getTableStr);
	gaCoreConfiguration.addParam('sysparm_field', getFieldStr);
	gaCoreConfiguration.addParam('sysparm_sysid', newValue);
	gaCoreConfiguration.addParam('sysparm_type', setTypeStr);
	gaCoreConfiguration.getXMLAnswer(callBackParse);
	
	// Callback function
	function callBackParse(response) {
		
		var answer = JSON.parse(response);
		
		if(setTypeStr == 'string') {
			g_form.setValue(setFieldStr, answer.value);
		}
		if(setTypeStr == 'reference') {
			g_form.setValue(setFieldStr, answer.value, answer.displayValue);
		}
		if(setTypeStr == 'displayValue') {
			g_form.setValue(setFieldStr, answer.displayValue);
		}
		
	}
	
}

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hello Mark, 

Used this script to populate a reference field with the value of a string field. Following modifications were done in the onChange Client script's line 4,5,7 and 8 as instructed - 

// Define variables -> Get

var getTableStr = 'u_masterci_demo_custom_table'; // Table
var getFieldStr = 'u_state_no_ref'; // Field


// Define variables -> Set
var setFieldStr = 'u_state'; // Field
var setTypeStr = 'reference'; // Type, 'reference' .OR. 'string' .OR. 'displayValue

Attached a screenshot - u_state_no_ref is a string field and u_state is reference field. 

The variable answer under the callBackParse function is NULL. I confirmed that the Script Include is client callable. Please help me out. Thanks.