How to get values to drop down field from reference field based on condition

Bhaskar24
Tera Expert

Hi All,

 

I have a requirement to display the values in drop down field: "CIs Assigned" based on the filed value of "Caller"

 

In detail, If Caller is "Zackary Mockus" the CIs assigned to him should be display under drop down field: "CIs Assigned"

 

find_real_file.png

 

find_real_file.png

 

So, the CIs: "MacBook Pro 17" and "Precision T5500 Workstation" should be displayed under the field: CIs Assigned below the -- None -- with the end value as "Create new CI"

 

So, the field: "CIs Assigned" drop-down values should be as below:

-- None --

MacBook Pro 17

Precision T5500 Workstation

Create new CI

 

 

Could you please help me on this!

 

Thanks

1 ACCEPTED SOLUTION

OK, I will do the job for you once. The Server Side Script could look like the following:

var MyCiHelper = Class.create();
MyCiHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getCisAssignedTo: function (userId) {
		var ids = [], gr = new GlideRecord("cmdb_ci");
		gr.addQuery("assigned_to", userId);
		gr.query();
		while(gr.next()) {
			ids.push({id: gr.getUniqueValue(), name: gr.getValue("name")});
		}
		return ids;
	},
	getCisAssignedToAsJson: function () {
		return JSON.stringify(this.getCisAssignedTo(this.getParameter("sysparm_userId")));
	},
    type: "MyCiHelper"
});

Let use the "CIs Assigned" field have the name u_cis_assigned. Then the Client Script could be the following:

function onChange (control, oldValue, newValue) {
	"use strict";
	g_form.clearOptions("u_cis_assigned");
	g_form.addOption("u_cis_assigned", "", "-- None --");
	if (newValue === "") {
		return;
	}

	var ga = new GlideAjax("MyCiHelper");

	ga.addParam("sysparm_name", "getCisAssignedToAsJson");
	ga.addParam("sysparm_userId", newValue);
	ga.getXMLAnswer(function (answer) {
		try {
			answer = JSON.parse(answer);
		} catch(unused) { }
		if (answer != null && Array.isArray(answer)) {
			answer.forEach(function (item) {
				g_form.addOption("u_cis_assigned", item.id, item.name);
			});
		}
	});
}

it should run onChange of Caller field (and on load the form too):

find_real_file.png

 As the result, the choice list (after successful loading of CIs, assigned to the caller) will be look like you expect:

find_real_file.png

View solution in original post

6 REPLIES 6

Oleg
Mega Sage

You should define "CIs Assigned" as the column of Reference type and to use "Advanced" reference qualified for "CIs Assigned" field and to use the following reference qualifier:

javascript: "assigned_to=" + current.caller_id

find_real_file.png

 

 

Type of the field: "CIs Assigned" should be dropdown but not reference. Could you please help me Thanks

I find the usage of dropdown not gut in the case because of many reasons. If you do need to use dropdown then you will be need to write some small code in Script Include, which uses "Client callable" checked. The script should return the list of the ids and names of Configuration Item filtered by CliendId and sorted by name. You should write small Client Script, which call the Script Include via Ajax request (for example using getXMLAnswer method of GlideAjax). Inside of the client script you should use g_form.addOption method to add the names/ids of Configuration Item to the "CIs Assigned". If you would like to use more comfortable version of dropdown then you should call select2() of the DOM element of dropdown to convert it to select2 control.

Thank for the reply. Could you please help me with the scrpt.