Mark Roethof
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