how to populate data dynamically into choice list which belongs to 2 tables

Renu3
Giga Contributor

Hello Everyone,

I am trying to populate data into choice field from the table and i have no idea how to do this.

My issue is: 

I would want to select the typer of share and when i click on file share in the second choice list i would want to populate the file path.(As shown in pic 1)

The issue here is i am using a custom table(pic 2) to save the file share information and the fields are on incident table.

Is there a way i can map these tables?

Or do i write a client script or use AJAX i have no idea.

Please guide me .

 

 

 

7 REPLIES 7

Brad Bowman
Kilo Patron
Kilo Patron

Hi Renu,

If I'm following this correctly, first you would create an onChange Client Script when your 'Select the file type' field changes.  The script will look something like this

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

  g_form.clearOptions('u_file_path');//replace with your field name
  var ga = new GlideAjax('fileAuthUtils');//name of Script Include
  ga.addParam('sysparm_name', 'getFilePathChoices');//name of function in SI
  ga.addParam('sysparm_fileType', newValue);
  ga.getXML(setFilePath);

  function setFilePath(response){
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    g_form.addOption('u_file_path','',' -- None --');//replace with your field name
    for(var i=0;i<answer.length;i++){
      g_form.addOption('u_file_path',answer[i].value,answer[i].label);//replace with your field name
    }
  }
}

If you only want this to trigger when the file type changes to File Share, you can add an if block.  Next, create a Script Include using the same name as in the GlideAjax call, ensuring the Client callable box is checked.  Your script will look similar to this

var fileAuthUtils = Class.create();
fileAuthUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
  getFilePathChoices : function(){
    var optionsArray = [];
    var processer = new JSON();
    var fa = new GlideRecord('u_file_authorizations');//replace with your custom table name
    fa.addQuery('u_share_type', this.getParameter('sysparm_fileType'));//replace with correct field name
    fa.query();
    while(fa.next()){
      optionsArray.push({
        'label' : fa.u_file_path.toString(),
        'value' : fa.u_file_path.toString()
      });//replace with the field name(s) for your choice list Label and Value
    }
  return processer.encode(optionsArray);
  },
  type: 'fileAuthUtils'
});

This will read the custom table and return to the client a list of labels and values which meet the query criteria.  The client script uses this list to add each to the drop-down.  This also assumes that the choice list Value for file type matches the label ('File Share') which matches the value in the share_type field in the custom table.

 

 

Hi Brad, 
Does the dynamic set of values and labels work for List type field ? 
I have array of data retrieved from SI and ready to be iterated on to get values and labels, but it doesn't work for List type field, or I am maybe messing something

Yep, having same problem. I have a List control that is on a table and is multi-select. The sys_choices are there and work fine if you open up that form and you see all the options.
Problem is that we want to dot-walk to this table from a parent table and the dot-walking clears out the choices, but the values come over. When the parent record is shown for the first time, it shows the labels, but if you edit the field (unlock) all the labels are replaced by values and then when you close it you only see the values (comma delimited). It looks like the label/value pairs are not coming across so I was hoping that something like this could be used to dynamically populate the table from the sys_choice table...but even using g_form.addOption manually is not giving me any more options.

Roger11_0-1702857931400.pngRoger11_1-1702857963797.png

 



Renu3
Giga Contributor

Hello Brad,

 

I tried implementing the above code with the modifications i dont see any changes happening except the choices changes to none in file path.

I have attached the results below could you please check and help.

Also would the same technique of calling work for other options also in filetype?

currently i have fileshare and sharepoint data to be sorted and pulled out but the choices will increase in future.

Note: i would want to use the same table for calling the information related to fileshares and sharepoint etc.