Display Data based on the previous selection

Priya Rao
Tera Contributor

Hi,

Here I'm trying to display the date based on the previous selection.

The data is stored in the custom table and mapped accordingly.

On-Load Catalog Client Script:

variable_1 - place - select box

variable_2 - name - select box

Based on the selection of place, I need to display the names.

table name - u_table

place field on table - location

name field on table - u_name

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var provide = g_form.getValue('name');
    var ga = new GlideAjax('populateData');
    ga.addParam("sysparm_name", "getName");
    ga.addParam("sysparm_id", provide);
    ga.getXML(getResponse);

    function getResponse(response) {

        var values = response.responseXML.documentElement.getAttribute('answer');
        var jsonObj = JSON.parse(values);
        var len = jsonObj.length;
        g_form.clearOptions('name');
        g_form.addOption("name", "", "-- None --");
        for (i = 0; i < len; i++) {
            g_form.addOption('name', jsonObj[i].sys_id, jsonObj[i].location);
        }
    }
}

 

Script Include:

 

var populateData = Class.create();
populateData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getName: function() {
		var res = [];
        var provide = this.getParameter("sysparm_id");
        var check = new GlideRecord("u_table");
        check.addEncodedQuery('u_nameISNOTEMPTY');
		//check.addQuery("sys_id", provide); 
        check.query();
        while (check.next()) {
            var resobj = {};
            resobj.sys_id = check.sys_id.toString();
			resobj.location = check.getElement('u_name').getDisplayValue();
            res.push(resobj);
			// res = check.u_name;
        }
        return JSON.stringify(res);
    },
    type: 'populateData'
});

 

With the above script it's displaying all the names.

Example: From the below table when India is selected in 'place', it should display A and B in the options for 'name' field and similarly when Canada is selected in 'place', it should display C, D and E in the options for 'name' field. Please let me know how can I achieve this

LocationName
IndiaA
IndiaB
CanadaC
CanadaD
CanadaE

@Rahul Talreja @Samaksh Wani @Pavankumar_1 @Sagar Pagar @Ravi Chandra_K @Ankur Bawiskar @Manmohan K 

Regards,

Priya Rao

2 ACCEPTED SOLUTIONS

@Priya Rao 

try this once

Also not sure why are you using onLoad; it should be onChange since on change of place you want options to be added dynamically

var populateData = Class.create();
populateData.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getName: function() {

		var place = this.getParameter('sysparm_place');
		var res = [];
		var check = new GlideRecord('u_table'); //table
		check.addEncodedQuery('u_nameISNOTEMPTY'); //name field on table
		check.addQuery('u_location.name', place); // give correct location field name here
		check.query();
		while (check.next()) {
			var resobj={};
			resobj.u_room_name = check.u_name.toString();
			res.push(resobj);
		}
		return JSON.stringify(res);
	},
	type: 'populateData'
});

onChange of place variable

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

	g_form.clearOptions('name'); // give here user_id variable name
	if(oldValue != newValue){
		var ga = new GlideAjax('populateData');
		ga.addParam('sysparm_name', "getName");
		ga.addParam('sysparm_place', newValue); // give here user_id variable name
		ga.getXMLAnswer(function(answer){
			g_form.addOption("name", "", "-- None --");
			var jsonObj = JSON.parse(answer);
			var len = jsonObj.length;
			for (i = 0; i < len; i++) {
				g_form.addOption('name', jsonObj[i].u_name, jsonObj[i].u_name);
			}
		});
		//Type appropriate comment here, and begin script below
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Rahul Talreja
Mega Sage
Mega Sage

Hi @Priya Rao ,
As per discussion, IG the issue is resolved.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul

View solution in original post

21 REPLIES 21

@Priya Rao 

Glad to know.

In this case there should be only 1 correct answer to your question which should be mine.

Please ensure you correct that so that it doesn't confuse other members in future.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Rahul Talreja
Mega Sage
Mega Sage

Hi @Priya Rao ,
As per discussion, IG the issue is resolved.

Please mark my response correct/helpful as applicable!
Thanks and Regards,
Rahul