Dynamically Populate a Field Based-on the Value(s) of Another Field

appstorm
Tera Contributor

I have been working on several on-change client scripts to make this work, but have so far been unsuccessful.  The task to make two fields that share the same table populate with their respective values, based-on the value of another field from the same table.

find_real_file.png

find_real_file.png

Below, is my script include.

find_real_file.png

 

How do I go about writing an on-change CS to make this work?

Thank you!

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

This is what I have defined in my PDI:

Hfind_real_file.pngHave added the fields to the Change Request form:

find_real_file.png

After flushing the cache, when I change System, Analyst and Owner automatically update.

View solution in original post

38 REPLIES 38

The field can be named anything, don't have to be the same as the referenced table at all. Of course form a maintenance/development perspective it is expected that a field that is a reference to table Owner to be called Owner, but that's all. From a functionality perspective there is absolutely no relation or dependency between a reference field's name and the table referenced. E.g. on User table the field manager points to "itself", is a reference to User, but it is called, well, Manager.

appstorm
Tera Contributor

Thanks so much!  This is making better sense.  I am still getting a "response is null" on System change, even after verifying all field names are correct.  I am going to step away for a few and approach this again with a clear brain... Hopefully that helps!

In the mean time you could post your code, maybe the fault will be spotted.

appstorm
Tera Contributor

Here it is...

CS:

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

	var ga = new GlideAjax('GetSysName'); // Script Include Name
	ga.addParam('sysparm_name', 'getName'); // SI Method
	ga.addParam('sysparm_system_unique_value', newValue); // Parameter for SI
	ga.getXMLAnswer(function (response) {
		alert('response is ' + response);
		
		var answer = JSON.parse(response);
		
		g_form.setValue('u_system_owner', answer.owner.uniqueValue, answer.owner.displayValue);
		g_form.setValue('u_analyst', answer.analyst.uniqueValue, answer.analyst.displayValue);
	});
}

SI:

GetSysName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getName: function () {
		var gn = {};
		var sn = this.getParameter('sysparm_system_unique_value');
		var rec = new GlideRecord('cmdb_application_product_model');

		if (rec.get(sn)) {
			gn.owner = {
				'uniqueValue': '' + rec.owner,
				'displayValue': rec.owner.getDisplayValue(),
			};
			gn.analyst = {
				'uniqueValue': '' + rec.u_analyst,
				'displayValue': rec.u_analyst.getDisplayValue(),
			};
		}

		return JSON.stringify(gn);
	},

	type: 'GetSysName'
});

Couple of things to verify:

- the name of the script include is the same as the name in code (GetSysName)

- the script include is marked as "Client callable"

- the script include begins with line

var GetSysName = Class.create();

A very big problem is that the method is called getName. In fact the following names should not be (re)used in Script Includes that are extending AbstractAjaxProcessor: CALLABLE_PREFIX, gc, getChars, getDocument, getName, getParameter, getRootElement, getType, getValue, newItem, process, request, responseXML, setAnswer, setError. This would be the 1st thing I'd correct. Creating functions with any of those names will render the Ajax handler Script Include non-functional.

Another potential problem could be field System on Change Request or rather how it is defined. It should be a reference to table cmdb_application_product_model - is it? In your very 1st screen-shot it is not. Have you modified that?