Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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

SanjivMeher
Mega Patron

Interesting. Why not use a lookup select box with reference qualifier instead of a client script?

In the reference qualifier, you can specify the table_name field to filter out field from sys_dictionary table?

Few examples
https://www.servicenow.com/community/developer-forum/scripting-a-reference-qualifier-for-lookup-sele...

https://www.servicenow.com/community/developer-forum/reference-qualifier-on-lookup-select-box-variab...

 


Please mark this response as correct or helpful if it assisted you with your question.

maddalahemanth
Tera Contributor

Hii @TEJAS  ,

Here is the on-change catalog client script and script include code in detail. Try this once

maddalahemanth_0-1742451868541.png

Here in this picture, you need to change the variable name to your variable name

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var val=g_form.getReference('table_name'); //In this line also you have to change table_name to your variable backend name
   g_form.addInfoMessage("executing script "+val);
   var ga=new GlideAjax('global.fields');
   ga.addParam('sysparm_name','getFields');
   ga.addParam('tablename',val.name);
   ga.getXMLAnswer(function(response)
   {
    var res=response.split(',');
    g_form.addInfoMessage(res);
    for(var i=0;i<res.length;i++)
    {

        g_form.addOption('condition_fields',i,res[i]); //In this line you have to replace the condition_fields to you varibale choice backend name
    }
   });
}

Here is the script include don't to change anything here

maddalahemanth_1-1742452059405.png

 

 

var fields = Class.create();
fields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getFields:function()
    {
        var tbl=this.getParameter('tablename');
        var ans=[];
        var gr=new GlideRecord('sys_dictionary');
        gr.addActiveQuery();
        gr.addQuery('name',tbl);
        gr.addQuery('internal_type!=collection');
        gr.query();
        gs.print(gr.getRowCount());
        while(gr.next())
        {
            ans.push(gr.column_label.toString());
        }
        ans=ans.join(',');
        gs.log('ans: '+ans);
        return ans;
    },
    type: 'fields'
});
 
Try this once and let me know if it works fine or not

Hi @maddalahemanth 
In my case it doesn't work because 
The scope which you have used is Global but in my case it is other scope so 
But also i tried its not working  

Hii @TEJAS  

I have set my script include accessible from all application scope then it will be accessed from different scopes also

Check it once or else can I know what is the error you are getting.

Hi @maddalahemanth ,
This the error i am getting "Messageexecuting script undefined"

This the onchange client script 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val=g_form.getReference('Table_Name'); //In this line also you have to change table_name to your variable backend name
g_form.addInfoMessage("executing script "+val);
var ga=new GlideAjax('x_syslt_datamask.fields');
ga.addParam('sysparm_name','getFields');
ga.addParam('tablename',val.name);
ga.getXMLAnswer(function(response)
{
var res=response.split(',');
g_form.addInfoMessage(res);
for(var i=0;i<res.length;i++)
{

g_form.addOption('condition_fields',i,res[i]); //In this line you have to replace the condition_fields to you varibale choice backend name
}
});
}

This the Script Include
var fields = Class.create();
fields.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getFields:function()
{
var tbl=this.getParameter('tablename');
var ans=[];
var gr=new GlideRecord('sys_dictionary');
gr.addActiveQuery();
gr.addQuery('name',tbl);
gr.addQuery('internal_type!=collection');
gr.query();
gs.print(gr.getRowCount());
while(gr.next())
{
ans.push(gr.column_label.toString());
}
ans=ans.join(',');
gs.log('ans: '+ans);
return ans;
},
type: 'fields'
});