Dynamically Populate Dropdown in Record Producer Based on Selected Table

TEJAS
Tera Contributor

Hi All,
Hope you are doing well,

 

Description:

I have created a Record Producer and need help dynamically populating a dropdown field based on the selected table.

Variables in the Record Producer:

  1. Table Name (Variable Name: table_name)

    • Type: Reference
    • Reference: sys_db_object (Tables)
    • Only specific tables should be visible its working.
      TEJAS_0-1742446423964.png

       



  2. Condition Fields (Variable Name: condition_fields)

    • Type: Select Box
    • Requirement:
      • When a user selects a table from the Table Name dropdown, the Condition Fields dropdown should dynamically populate the fields of the selected table.
      • This should happen using an onChange Client Script.
        TEJAS_1-1742446448223.png

        IMP: one more thing this is done in other scope not in global scope
        Thanks in advance 



1 ACCEPTED SOLUTION

GopikaP
Mega Sage

Hi @TEJAS  , 

  1. Create an onChange Client Script on master table -
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
	var table = g_form.getReference('master_table');
    var ga = new GlideAjax('ClientSideCall');
    ga.addParam('sysparm_name', 'getFieldName');
    ga.addParam('sysparm_tableName',table.name);
    ga.getXML(getFields);
    function getFields(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var fields = answer.split(',');
        for (var i = 0; i < fields.length; i++) {
            g_form.addOption('select_fields', i, fields[i]);
        }
    }
}​
  • Create a Glide AJAX enabled script include - 
var ClientSideCall = Class.create();
ClientSideCall.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getFieldName: function() {
        var fields = [];
        var tableName = this.getParameter('sysparm_tableName');
		gs.log("Hi "+JSON.stringify(tableName));
        var getFieldNames = new GlideRecord('sys_dictionary');
        getFieldNames.addQuery('name', tableName);
        getFieldNames.query();
        while (getFieldNames.next()) {
            fields.push(getFieldNames.getValue('column_label'));
        }
		gs.log("Hi "+fields);
        fields = fields.join(',');
        return fields;
    },
    type: 'ClientSideCall'
});​

View solution in original post

21 REPLIES 21

TEJAS
Tera Contributor

Sorry for the problem 

 Table Name (Variable Name: Table_Name)
it is Table_Name
and one more thing now i saw i am getting this error in the onchange client script 

TEJAS_1-1742451963111.png

 

 

Hi @TEJAS  , did you do any kind of debugging, any brackets left out? I am not able to reproduce the error in my instance.

TEJAS
Tera Contributor

Now i am getting 
Thank @GopikaP 

Ankur Bawiskar
Tera Patron
Tera Patron

@TEJAS  

so please share what script did you start with?

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

@TEJAS  

why not use Reference field to sys_dictionary and show the fields based on above table selected?

It's not a good user experience to show 100 fields if that table has 100 fields

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